]> wimlib.net Git - wimlib/blobdiff - src/util.c
Add memdup() function
[wimlib] / src / util.c
index f7ed251c784868028bf12ef2be01cd36221738d8..a0ed84d95014760cff7f0b95618c1fcb78c1d943 100644 (file)
@@ -414,7 +414,10 @@ static void *(*wimlib_realloc_func)(void *, size_t) = realloc;
 void *
 wimlib_malloc(size_t size)
 {
-       return (*wimlib_malloc_func)(size);
+       void *ptr = (*wimlib_malloc_func)(size);
+       if (ptr == NULL && size != 0)
+               ERROR("memory exhausted");
+       return ptr;
 }
 
 void
@@ -426,7 +429,10 @@ wimlib_free_memory(void *ptr)
 void *
 wimlib_realloc(void *ptr, size_t size)
 {
-       return (*wimlib_realloc_func)(ptr, size);
+       ptr = (*wimlib_realloc_func)(ptr, size);
+       if (ptr == NULL && size != 0)
+               ERROR("memory exhausted");
+       return ptr;
 }
 
 void *
@@ -469,6 +475,15 @@ wimlib_wcsdup(const wchar_t *str)
 
 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
 
+void *
+memdup(const void *mem, size_t size)
+{
+       void *ptr = MALLOC(size);
+       if (ptr)
+               ptr = memcpy(ptr, mem, size);
+       return ptr;
+}
+
 WIMLIBAPI int
 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
                            void (*free_func)(void *),