debuggers.hg

view tools/libxl/libxl_internal.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents dd9250567d18
children 5993c6b9f4e5
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 "libxl.h"
23 #include "libxl_internal.h"
24 #include "libxl_utils.h"
26 int libxl_error_set(struct libxl_ctx *ctx, int code)
27 {
28 return 0;
29 }
31 int libxl_ptr_add(struct libxl_ctx *ctx, void *ptr)
32 {
33 int i;
34 void **re;
36 if (!ptr)
37 return 0;
39 /* fast case: we have space in the array for storing the pointer */
40 for (i = 0; i < ctx->alloc_maxsize; i++) {
41 if (!ctx->alloc_ptrs[i]) {
42 ctx->alloc_ptrs[i] = ptr;
43 return 0;
44 }
45 }
46 /* realloc alloc_ptrs manually with calloc/free/replace */
47 re = calloc(ctx->alloc_maxsize + 25, sizeof(void *));
48 if (!re)
49 return -1;
50 for (i = 0; i < ctx->alloc_maxsize; i++)
51 re[i] = ctx->alloc_ptrs[i];
52 /* assign the next pointer */
53 re[i] = ptr;
55 /* replace the old alloc_ptr */
56 free(ctx->alloc_ptrs);
57 ctx->alloc_ptrs = re;
58 ctx->alloc_maxsize += 25;
59 return 0;
60 }
62 int libxl_free(struct libxl_ctx *ctx, void *ptr)
63 {
64 int i;
66 if (!ptr)
67 return 0;
69 /* remove the pointer from the tracked ptrs */
70 for (i = 0; i < ctx->alloc_maxsize; i++) {
71 if (ctx->alloc_ptrs[i] == ptr) {
72 ctx->alloc_ptrs[i] = NULL;
73 free(ptr);
74 return 0;
75 }
76 }
77 /* haven't find the pointer, really bad */
78 return -1;
79 }
81 int libxl_free_all(struct libxl_ctx *ctx)
82 {
83 void *ptr;
84 int i;
86 for (i = 0; i < ctx->alloc_maxsize; i++) {
87 ptr = ctx->alloc_ptrs[i];
88 ctx->alloc_ptrs[i] = NULL;
89 free(ptr);
90 }
91 return 0;
92 }
94 void *libxl_zalloc(struct libxl_ctx *ctx, int bytes)
95 {
96 void *ptr = calloc(bytes, 1);
97 if (!ptr) {
98 libxl_error_set(ctx, ENOMEM);
99 return NULL;
100 }
102 libxl_ptr_add(ctx, ptr);
103 return ptr;
104 }
106 void *libxl_calloc(struct libxl_ctx *ctx, size_t nmemb, size_t size)
107 {
108 void *ptr = calloc(nmemb, size);
109 if (!ptr) {
110 libxl_error_set(ctx, ENOMEM);
111 return NULL;
112 }
114 libxl_ptr_add(ctx, ptr);
115 return ptr;
116 }
118 char *libxl_sprintf(struct libxl_ctx *ctx, const char *fmt, ...)
119 {
120 char *s;
121 va_list ap;
122 int ret;
124 va_start(ap, fmt);
125 ret = vsnprintf(NULL, 0, fmt, ap);
126 va_end(ap);
128 if (ret < 0) {
129 return NULL;
130 }
132 s = libxl_zalloc(ctx, ret + 1);
133 if (s) {
134 va_start(ap, fmt);
135 ret = vsnprintf(s, ret + 1, fmt, ap);
136 va_end(ap);
137 }
138 return s;
139 }
141 char *libxl_dirname(struct libxl_ctx *ctx, const char *s)
142 {
143 char *c;
144 char *ptr = libxl_sprintf(ctx, "%s", s);
146 c = strrchr(ptr, '/');
147 if (!c)
148 return NULL;
149 *c = '\0';
150 return ptr;
151 }
153 void xl_logv(struct libxl_ctx *ctx, int loglevel, int errnoval,
154 const char *file, int line, const char *func,
155 char *fmt, va_list ap)
156 {
157 char *enomem = "[out of memory formatting log message]";
158 char *s;
159 int rc;
161 if (!ctx->log_callback)
162 return;
164 rc = vasprintf(&s, fmt, ap);
165 if (rc<0) { s = enomem; goto x; }
167 if (errnoval >= 0) {
168 char *errstr, *snew;
169 errstr = strerror(errnoval);
170 if (errstr)
171 rc = asprintf(&snew, "%s: %s", s, errstr);
172 else
173 rc = asprintf(&snew, "%s: unknown error number %d", s, errnoval);
174 free(s);
175 if (rc<0) { s = enomem; goto x; }
176 s = snew;
177 }
179 x:
180 ctx->log_callback(ctx->log_userdata, loglevel, file, line, func, s);
181 if (s != enomem)
182 free(s);
183 }
185 void xl_log(struct libxl_ctx *ctx, int loglevel, int errnoval,
186 const char *file, int line,
187 const char *func, char *fmt, ...)
188 {
189 va_list ap;
190 va_start(ap, fmt);
191 xl_logv(ctx, loglevel, errnoval, file, line, func, fmt, ap);
192 va_end(ap);
193 }