debuggers.hg

view tools/libxl/libxl_internal.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 4ed9f9218620
children
line source
1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; version 2.1 only. with the special
8 * exception on linking described in file LICENSE.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 */
16 #include "libxl_osdeps.h"
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
28 #include "libxl.h"
29 #include "libxl_internal.h"
30 #include "libxl_utils.h"
32 int libxl__error_set(libxl_ctx *ctx, int code)
33 {
34 return 0;
35 }
37 int libxl__ptr_add(libxl__gc *gc, void *ptr)
38 {
39 int i;
40 void **re;
42 if (!ptr)
43 return 0;
45 /* fast case: we have space in the array for storing the pointer */
46 for (i = 0; i < gc->alloc_maxsize; i++) {
47 if (!gc->alloc_ptrs[i]) {
48 gc->alloc_ptrs[i] = ptr;
49 return 0;
50 }
51 }
52 /* realloc alloc_ptrs manually with calloc/free/replace */
53 re = calloc(gc->alloc_maxsize + 25, sizeof(void *));
54 if (!re)
55 return -1;
56 for (i = 0; i < gc->alloc_maxsize; i++)
57 re[i] = gc->alloc_ptrs[i];
58 /* assign the next pointer */
59 re[i] = ptr;
61 /* replace the old alloc_ptr */
62 free(gc->alloc_ptrs);
63 gc->alloc_ptrs = re;
64 gc->alloc_maxsize += 25;
65 return 0;
66 }
68 void libxl__free_all(libxl__gc *gc)
69 {
70 void *ptr;
71 int i;
73 for (i = 0; i < gc->alloc_maxsize; i++) {
74 ptr = gc->alloc_ptrs[i];
75 gc->alloc_ptrs[i] = NULL;
76 free(ptr);
77 }
78 free(gc->alloc_ptrs);
79 }
81 void *libxl__zalloc(libxl__gc *gc, int bytes)
82 {
83 void *ptr = calloc(bytes, 1);
84 if (!ptr) {
85 libxl__error_set(libxl__gc_owner(gc), ENOMEM);
86 return NULL;
87 }
89 libxl__ptr_add(gc, ptr);
90 return ptr;
91 }
93 void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size)
94 {
95 void *ptr = calloc(nmemb, size);
96 if (!ptr) {
97 libxl__error_set(libxl__gc_owner(gc), ENOMEM);
98 return NULL;
99 }
101 libxl__ptr_add(gc, ptr);
102 return ptr;
103 }
105 char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...)
106 {
107 char *s;
108 va_list ap;
109 int ret;
111 va_start(ap, fmt);
112 ret = vsnprintf(NULL, 0, fmt, ap);
113 va_end(ap);
115 if (ret < 0) {
116 return NULL;
117 }
119 s = libxl__zalloc(gc, ret + 1);
120 if (s) {
121 va_start(ap, fmt);
122 ret = vsnprintf(s, ret + 1, fmt, ap);
123 va_end(ap);
124 }
125 return s;
126 }
128 char *libxl__strdup(libxl__gc *gc, const char *c)
129 {
130 char *s = strdup(c);
132 if (s)
133 libxl__ptr_add(gc, s);
135 return s;
136 }
138 char *libxl__dirname(libxl__gc *gc, const char *s)
139 {
140 char *c;
141 char *ptr = libxl__strdup(gc, s);
143 c = strrchr(ptr, '/');
144 if (!c)
145 return NULL;
146 *c = '\0';
147 return ptr;
148 }
150 void libxl__logv(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
151 const char *file, int line, const char *func,
152 char *fmt, va_list ap)
153 {
154 char *enomem = "[out of memory formatting log message]";
155 char *base = NULL;
156 int rc, esave;
157 char fileline[256];
159 esave = errno;
161 rc = vasprintf(&base, fmt, ap);
162 if (rc<0) { base = enomem; goto x; }
164 fileline[0] = 0;
165 if (file) snprintf(fileline, sizeof(fileline), "%s:%d",file,line);
166 fileline[sizeof(fileline)-1] = 0;
168 x:
169 xtl_log(ctx->lg, msglevel, errnoval, "libxl",
170 "%s%s%s%s" "%s",
171 fileline, func&&file?":":"", func?func:"", func||file?" ":"",
172 base);
173 if (base != enomem) free(base);
174 errno = esave;
175 }
177 void libxl__log(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
178 const char *file, int line, const char *func,
179 char *fmt, ...)
180 {
181 va_list ap;
182 va_start(ap, fmt);
183 libxl__logv(ctx, msglevel, errnoval, file, line, func, fmt, ap);
184 va_end(ap);
185 }
187 char *libxl__abs_path(libxl__gc *gc, char *s, const char *path)
188 {
189 if (!s || s[0] == '/')
190 return s;
191 return libxl__sprintf(gc, "%s/%s", path, s);
192 }
195 int libxl__file_reference_map(libxl_file_reference *f)
196 {
197 struct stat st_buf;
198 int ret, fd;
199 void *data;
201 if (f->mapped)
202 return 0;
204 fd = open(f->path, O_RDONLY);
205 if (f < 0)
206 return ERROR_FAIL;
208 ret = fstat(fd, &st_buf);
209 if (ret < 0)
210 goto out;
212 ret = -1;
213 data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
214 if (data == NULL)
215 goto out;
217 f->mapped = 1;
218 f->data = data;
219 f->size = st_buf.st_size;
221 ret = 0;
222 out:
223 close(fd);
225 return ret == 0 ? 0 : ERROR_FAIL;
226 }
228 int libxl__file_reference_unmap(libxl_file_reference *f)
229 {
230 int ret;
232 if (!f->mapped)
233 return 0;
235 ret = munmap(f->data, f->size);
237 return ret == 0 ? 0 : ERROR_FAIL;
238 }