static object_t *buildNumber(parser_state_t *state, object_t *o);
static object_t *buildBoolean(parser_state_t *state, object_t *o);
-#include <libkern/OSRuntime.h>
+__BEGIN_DECLS
+#include <kern/kalloc.h>
+__END_DECLS
-#define malloc(s) kern_os_malloc(s)
-#define realloc(a, s) kern_os_realloc(a, s)
-#define free(a) kern_os_free((void *)a)
+#define malloc(size) malloc_impl(size)
+static inline void *
+malloc_impl(size_t size)
+{
+ if (size == 0) {
+ return NULL;
+ }
+ return kheap_alloc_tag_bt(KHEAP_DEFAULT, size,
+ (zalloc_flags_t) (Z_WAITOK | Z_ZERO),
+ VM_KERN_MEMORY_LIBKERN);
+}
+
+#define free(addr) free_impl(addr)
+static inline void
+free_impl(void *addr)
+{
+ kheap_free_addr(KHEAP_DEFAULT, addr);
+}
+static inline void
+safe_free(void *addr, size_t size)
+{
+ if(addr) {
+ assert(size != 0);
+ kheap_free(KHEAP_DEFAULT, addr, size);
+ }
+}
+
+#define realloc(addr, osize, nsize) realloc_impl(addr, osize, nsize)
+static inline void *
+realloc_impl(void *addr, size_t osize, size_t nsize)
+{
+ if (!addr) {
+ return malloc(nsize);
+ }
+ if (nsize == osize) {
+ return addr;
+ }
+ void *nmem = malloc(nsize);
+ if (!nmem) {
+ safe_free(addr, osize);
+ return NULL;
+ }
+ (void)memcpy(nmem, addr, (nsize > osize) ? osize : nsize);
+ safe_free(addr, osize);
+
+ return nmem;
+}
%}
%token ARRAY
}
static char *
-getString(parser_state_t *state)
+getString(parser_state_t *state, int *alloc_lengthp)
{
int c = currentChar();
int start, length, i, j;
printf("OSUnserializeXML: can't alloc temp memory\n");
goto error;
}
+ if (alloc_lengthp != NULL) {
+ *alloc_lengthp = length + 1;
+ }
// copy out string in tempString
// "&" -> '&', "<" -> '<', ">" -> '>'
error:
if (tempString) {
- free(tempString);
+ safe_free(tempString, length + 1);
+ if (alloc_lengthp != NULL) {
+ *alloc_lengthp = 0;
+ }
}
return 0;
}
{
int numeq = 0, cntr = 0;
unsigned int acc = 0;
- int tmpbufpos = 0, tmpbuflen = 0;
- unsigned char *tmpbuf = (unsigned char *)malloc(DATA_ALLOC_SIZE);
+ int tmpbufpos = 0;
+ size_t tmpbuflen = DATA_ALLOC_SIZE;
+ unsigned char *tmpbuf = (unsigned char *)malloc(tmpbuflen);
int c = currentChar();
*size = 0;
while (c != '<') {
c &= 0x7f;
if (c == 0) {
- free(tmpbuf);
+ safe_free(tmpbuf, tmpbuflen);
return 0;
}
if (c == '=') {
acc += __CFPLDataDecodeTable[c];
if (0 == (cntr & 0x3)) {
if (tmpbuflen <= tmpbufpos + 2) {
+ size_t oldsize = tmpbuflen;
tmpbuflen += DATA_ALLOC_SIZE;
- tmpbuf = (unsigned char *)realloc(tmpbuf, tmpbuflen);
+ tmpbuf = (unsigned char *)realloc(tmpbuf, oldsize, tmpbuflen);
}
tmpbuf[tmpbufpos++] = (acc >> 16) & 0xff;
if (numeq < 2) {
}
*size = tmpbufpos;
if (*size == 0) {
- free(tmpbuf);
+ safe_free(tmpbuf, tmpbuflen);
return 0;
}
return tmpbuf;
int c;
unsigned char *d, *start, *lastStart;
- start = lastStart = d = (unsigned char *)malloc(DATA_ALLOC_SIZE);
+ size_t buflen = DATA_ALLOC_SIZE;
+ start = lastStart = d = (unsigned char *)malloc(buflen);
c = currentChar();
while (c != '<') {
d++;
if ((d - lastStart) >= DATA_ALLOC_SIZE) {
int oldsize = d - start;
- start = (unsigned char *)realloc(start, oldsize + DATA_ALLOC_SIZE);
+ assert(oldsize == buflen);
+ buflen += DATA_ALLOC_SIZE;
+ start = (unsigned char *)realloc(start, oldsize, buflen);
d = lastStart = start + oldsize;
}
c = nextChar();
error:
*size = 0;
- free(start);
+ safe_free(start, buflen);
return 0;
}
char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
object_t *object;
+ int alloc_length;
top:
c = currentChar();
if (tagType == TAG_EMPTY) {
return SYNTAX_ERROR;
}
- object->string = getString(STATE);
+ object->string = getString(STATE, &alloc_length);
if (!object->string) {
return SYNTAX_ERROR;
}
+ object->string_alloc_length = alloc_length;
if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
|| strcmp(tag, "key")) {
return SYNTAX_ERROR;
if (tagType == TAG_EMPTY) {
object->string = (char *)malloc(1);
object->string[0] = 0;
+ object->string_alloc_length = 1;
return STRING;
}
- object->string = getString(STATE);
+ object->string = getString(STATE, &alloc_length);
if (!object->string) {
return SYNTAX_ERROR;
}
+ object->string_alloc_length = alloc_length;
if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
|| strcmp(tag, "string")) {
return SYNTAX_ERROR;
} else {
o = (object_t *)malloc(sizeof(object_t));
// object_count++;
- bzero(o, sizeof(object_t));
o->free = state->objects;
state->objects = o;
}
t = o;
o = o->free;
- free(t);
+ safe_free(t, sizeof(object_t));
// object_count--;
}
// printf("object_count = %d\n", object_count);
rememberObject(state, o->idref, symbol);
}
- free(o->string);
+ safe_free(o->string, strlen(o->string) + 1);
o->string = 0;
o->object = symbol;
cleanupObjects(state);
state->tags->release();
- free(state);
+ safe_free(state, sizeof(parser_state_t));
return object;
}