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/.
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
36 #include "wimlib/assert.h"
37 #include "wimlib/error.h"
38 #include "wimlib/timestamp.h"
39 #include "wimlib/util.h"
40 #include "wimlib/xml.h"
46 static void *(*wimlib_malloc_func) (size_t) = malloc;
47 static void (*wimlib_free_func) (void *) = free;
48 static void *(*wimlib_realloc_func)(void *, size_t) = realloc;
51 wimlib_malloc(size_t size)
56 ptr = (*wimlib_malloc_func)(size);
67 wimlib_free_memory(void *ptr)
69 (*wimlib_free_func)(ptr);
73 wimlib_realloc(void *ptr, size_t size)
77 return (*wimlib_realloc_func)(ptr, size);
81 wimlib_calloc(size_t nmemb, size_t size)
83 size_t total_size = nmemb * size;
84 void *p = MALLOC(total_size);
86 p = memset(p, 0, total_size);
91 wimlib_strdup(const char *str)
99 p = memcpy(p, str, size + 1);
105 wimlib_wcsdup(const wchar_t *str)
111 p = MALLOC((size + 1) * sizeof(wchar_t));
113 p = wmemcpy(p, str, size + 1);
119 wimlib_aligned_malloc(size_t size, size_t alignment)
121 wimlib_assert(alignment != 0 && is_power_of_2(alignment) &&
124 const uintptr_t mask = alignment - 1;
128 raw_ptr = MALLOC(mask + sizeof(size_t) + size);
130 ptr = (char *)raw_ptr + sizeof(size_t);
131 ptr = (void *)(((uintptr_t)ptr + mask) & ~mask);
132 *((size_t *)ptr - 1) = ptr - raw_ptr;
138 wimlib_aligned_free(void *ptr)
141 FREE((char *)ptr - *((size_t *)ptr - 1));
145 memdup(const void *mem, size_t size)
147 void *ptr = MALLOC(size);
149 ptr = memcpy(ptr, mem, size);
153 /* API function documented in wimlib.h */
155 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
156 void (*free_func)(void *),
157 void *(*realloc_func)(void *, size_t))
159 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
160 wimlib_free_func = free_func ? free_func : free;
161 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
163 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
164 wimlib_realloc_func);
173 void *mempcpy(void *dst, const void *src, size_t n)
175 return memcpy(dst, src, n) + n;
179 static bool seeded = false;
184 srand(now_as_wim_timestamp());
188 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
190 randomize_char_array_with_alnum(tchar *p, size_t n)
205 /* Fills @n bytes pointer to by @p with random numbers. */
207 randomize_byte_array(u8 *p, size_t n)
217 get_available_cpus(void)
219 long n = sysconf(_SC_NPROCESSORS_ONLN);
220 if (n < 1 || n >= UINT_MAX) {
221 WARNING("Failed to determine number of processors; assuming 1.");
226 #endif /* !__WIN32__ */
230 get_available_memory(void)
232 #if defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES)
233 long page_size = sysconf(_SC_PAGESIZE);
234 long num_pages = sysconf(_SC_PHYS_PAGES);
235 if (page_size <= 0 || num_pages <= 0)
237 return ((u64)page_size * (u64)num_pages);
239 int mib[2] = {CTL_HW, HW_MEMSIZE};
241 size_t len = sizeof(memsize);
242 if (sysctl(mib, ARRAY_LEN(mib), &memsize, &len, NULL, 0) < 0 || len != 8)
248 WARNING("Failed to determine available memory; assuming 1 GiB");
251 #endif /* !__WIN32__ */