2 * util.c - utility functions
6 * Copyright (C) 2012, 2013, 2014 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/assert.h"
32 #include "wimlib/timestamp.h"
33 #include "wimlib/util.h"
34 #include "wimlib/xml.h"
40 static void *(*wimlib_malloc_func) (size_t) = malloc;
41 static void (*wimlib_free_func) (void *) = free;
42 static void *(*wimlib_realloc_func)(void *, size_t) = realloc;
45 wimlib_malloc(size_t size)
50 ptr = (*wimlib_malloc_func)(size);
61 wimlib_free_memory(void *ptr)
63 (*wimlib_free_func)(ptr);
67 wimlib_realloc(void *ptr, size_t size)
71 return (*wimlib_realloc_func)(ptr, size);
75 wimlib_calloc(size_t nmemb, size_t size)
77 size_t total_size = nmemb * size;
78 void *p = MALLOC(total_size);
80 p = memset(p, 0, total_size);
85 wimlib_strdup(const char *str)
93 p = memcpy(p, str, size + 1);
99 wimlib_wcsdup(const wchar_t *str)
105 p = MALLOC((size + 1) * sizeof(wchar_t));
107 p = wmemcpy(p, str, size + 1);
113 wimlib_aligned_malloc(size_t size, size_t alignment)
119 wimlib_assert(alignment != 0 && is_power_of_2(alignment) &&
121 mask = alignment - 1;
123 raw_ptr = MALLOC(size + alignment - 1 + sizeof(size_t));
127 ptr = (u8 *)raw_ptr + sizeof(size_t);
128 while ((uintptr_t)ptr & mask)
130 *((size_t *)ptr - 1) = (ptr - raw_ptr);
136 wimlib_aligned_free(void *ptr)
139 FREE((u8 *)ptr - *((size_t *)ptr - 1));
143 memdup(const void *mem, size_t size)
145 void *ptr = MALLOC(size);
147 ptr = memcpy(ptr, mem, size);
151 /* API function documented in wimlib.h */
153 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
154 void (*free_func)(void *),
155 void *(*realloc_func)(void *, size_t))
157 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
158 wimlib_free_func = free_func ? free_func : free;
159 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
161 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
162 wimlib_realloc_func);
171 void *mempcpy(void *dst, const void *src, size_t n)
173 return memcpy(dst, src, n) + n;
178 utf16le_strlen(const utf16lechar *s)
180 const utf16lechar *p = s;
183 return (p - s) * sizeof(utf16lechar);
186 static bool seeded = false;
191 srand(now_as_wim_timestamp());
195 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
197 randomize_char_array_with_alnum(tchar *p, size_t n)
212 /* Fills @n bytes pointer to by @p with random numbers. */
214 randomize_byte_array(u8 *p, size_t n)