Allow multiple atomic transactions to be used with one root object.
This allows transactions that include barriers to be performed
while holding an exclusive system lock.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011, 2012 J. Ali Harlow <ali@juiblex.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
37 #include <sys/utsname.h>
45 #include "razor-internal.h"
51 /* Required by gnulib on non-libc platforms */
52 char *program_name = "librazor";
66 razor_file_get_contents(const char *filename, size_t *length, int private,
67 struct razor_error **error)
75 fd = open(filename, O_RDONLY | O_BINARY);
77 razor_set_error(error, filename, strerror(errno));
81 if (fstat(fd, &st) < 0) {
82 razor_set_error(error, filename, strerror(errno));
90 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
91 if (addr == MAP_FAILED)
96 addr = malloc(st.st_size);
99 while(nb < st.st_size) {
100 res = read(fd, addr + nb, st.st_size - nb);
102 razor_set_error(error, filename,
111 razor_set_error(error, NULL, "Not enough memory");
118 int razor_file_free_contents(void *addr, size_t length)
121 return munmap(addr, length);
128 struct qsort_context {
130 razor_compare_with_data_func_t compare;
135 qsort_swap(void *p1, void *p2, size_t size)
140 memcpy(buffer, p1, size);
141 memcpy(p1, p2, size);
142 memcpy(p2, buffer, size);
147 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
148 struct qsort_context *ctx)
150 void *p, *start, *end, *pivot;
151 uint32_t *mp, *mstart, *mend, tmp;
152 int left, right, result;
153 size_t size = ctx->size;
157 end = base + nelem * size;
161 pivot = base + (rand() % nelem) * size;
164 result = ctx->compare(p, pivot, ctx->data);
166 qsort_swap(p, start, size);
176 } else if (result == 0) {
182 qsort_swap(p, end, size);
191 left = (start - base) / size;
192 right = (base + nelem * size - end) / size;
194 __qsort_with_data(base, left, map, ctx);
196 __qsort_with_data(end, right, mend, ctx);
200 razor_qsort_with_data(void *base, size_t nelem, size_t size,
201 razor_compare_with_data_func_t compare, void *data)
203 struct qsort_context ctx;
211 ctx.compare = compare;
214 map = malloc(nelem * sizeof (uint32_t));
215 for (i = 0; i < nelem; i++)
218 __qsort_with_data(base, nelem, map, &ctx);
223 void environment_init(struct environment *env)
226 array_init(&env->string_pool);
227 array_init(&env->vars);
230 void environment_add_variable(struct environment *env,
231 const char *variable, const char *value)
235 assert(!env->is_set);
237 s = array_add(&env->string_pool,
238 strlen(variable) + strlen(value) + 2);
239 sprintf(s, "%s=%s", variable, value);
240 r = array_add(&env->vars, sizeof *r);
241 *r = s - (char *)env->string_pool.data;
244 void environment_set(struct environment *env)
251 count = env->vars.size / sizeof(uint32_t);
252 r = (uint32_t *)env->vars.data;
253 for (i = 0; i < count; i++) {
254 s = env->string_pool.data + *r++;
269 void environment_unset(struct environment *env)
276 count = env->vars.size / sizeof(uint32_t);
277 r = (uint32_t *)env->vars.data;
278 for (i = 0; i < count; i++) {
279 s = env->string_pool.data + *r++;
299 void environment_release(struct environment *env)
301 environment_unset(env);
302 array_release(&env->string_pool);
303 array_release(&env->vars);
306 RAZOR_EXPORT char *razor_concat(const char *s, ...)
316 while((string = va_arg(args, const char *)))
317 len += strlen(string);
321 concat = malloc(len + 1);
329 memcpy(concat, s, len);
331 while((string = va_arg(args, const char *))) {
332 len = strlen(string);
333 memcpy(concat + n, string, len);
344 RAZOR_EXPORT const char *razor_system_arch(void)
349 GetNativeSystemInfo(&si);
350 switch(si.wProcessorArchitecture)
352 case PROCESSOR_ARCHITECTURE_INTEL:
354 case PROCESSOR_ARCHITECTURE_AMD64:
360 static struct utsname un;
371 char *razor_utf16_to_utf8(const wchar_t *utf16, int len)
376 n = WideCharToMultiByte(CP_UTF8, 0, utf16, len, NULL, 0, NULL, NULL);
377 if (len >= 0 && utf16[len])
380 (void)WideCharToMultiByte(CP_UTF8, 0, utf16, len, utf8, n, NULL, NULL);
381 if (len >= 0 && utf16[len])
387 wchar_t *razor_utf8_to_utf16(const char *utf8, int len)
392 n = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
393 if (len >= 0 && utf8[len])
395 utf16 = malloc(n * sizeof(wchar_t));
396 (void)MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, n);
397 if (len >= 0 && utf8[len])
403 #endif /* MSWIN_API */