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)
115 wimlib_assert(alignment != 0 && is_power_of_2(alignment) &&
118 const uintptr_t mask = alignment - 1;
122 raw_ptr = MALLOC(mask + sizeof(size_t) + size);
124 ptr = (char *)raw_ptr + sizeof(size_t);
125 ptr = (void *)(((uintptr_t)ptr + mask) & ~mask);
126 *((size_t *)ptr - 1) = ptr - raw_ptr;
132 wimlib_aligned_free(void *ptr)
135 FREE((char *)ptr - *((size_t *)ptr - 1));
139 memdup(const void *mem, size_t size)
141 void *ptr = MALLOC(size);
143 ptr = memcpy(ptr, mem, size);
147 /* API function documented in wimlib.h */
149 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
150 void (*free_func)(void *),
151 void *(*realloc_func)(void *, size_t))
153 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
154 wimlib_free_func = free_func ? free_func : free;
155 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
157 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
158 wimlib_realloc_func);
167 void *mempcpy(void *dst, const void *src, size_t n)
169 return memcpy(dst, src, n) + n;
174 utf16le_strlen(const utf16lechar *s)
176 const utf16lechar *p = s;
179 return (p - s) * sizeof(utf16lechar);
182 static bool seeded = false;
187 srand(now_as_wim_timestamp());
191 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
193 randomize_char_array_with_alnum(tchar *p, size_t n)
208 /* Fills @n bytes pointer to by @p with random numbers. */
210 randomize_byte_array(u8 *p, size_t n)