__private_extern__ void ptrlist_free(ptrlist_t * list) { if (list->array) free(list->array); ptrlist_init(list); return; }
static void ptrlist_free(ptrlist_t * list) { if (list->array) my_free(list->array); ptrlist_init(list); return; }
int xml_script_reader_parse(script_t *script) { assert(_p_xml_reader); /* init list of playlets */ int idx = 0; text_t *cur_text = NULL; ptrlist_init(&(script->texts)); int ret; int element_type = XML_READER_TYPE_NONE; unsigned char *element_name = '\0'; ret = xmlTextReaderRead(_p_xml_reader); while (ret == 1) { element_type = xmlTextReaderNodeType(_p_xml_reader); if (XML_READER_TYPE_ELEMENT == element_type) { element_name = xmlTextReaderLocalName(_p_xml_reader); if(!strcmp(XML_POINTER_ELE, (const char *) element_name)) { /* starting new text */ cur_text = text_init(); /* setting pointer index value. */ cur_text->pointer_idx = idx++; /* setting cpu address value. */ readPointerAddressAttr(&(cur_text->cpu_address)); } xmlFree(element_name); } else if (XML_READER_TYPE_END_ELEMENT == element_type) { element_name = xmlTextReaderLocalName(_p_xml_reader); if(!strcmp(XML_POINTER_ELE, (const char *) element_name)) { /* end of text*/ if(NULL != cur_text) ptrlist_add(&(script->texts), cur_text); cur_text = NULL; } xmlFree(element_name); } else if (XML_READER_TYPE_CDATA == element_type && NULL != cur_text) { readPointerCData(&(cur_text->u8srt)); } ret = xmlTextReaderRead(_p_xml_reader); } return 0; }
const char* /*UNITTEST*/ test_insert(void) { ptrlist_t l = ptrlist_init(); int w, x, y, z; int *p = &w, *q = &x, *r = &y, *s = &z; ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != s || ptrlist_get(l, 4) != NULL) return "got something else out than what was put in"; ptrlist_clear(l); ptrlist_insert(l, -1, p); ptrlist_insert(l, -1, q); ptrlist_insert(l, -1, r); ptrlist_insert(l, -1, s); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != s || ptrlist_get(l, 4) != NULL) return "got something else out than what was put in"; ptrlist_clear(l); ptrlist_insert(l, 5, r); //tail ptrlist_insert(l, -1, s); //tail ptrlist_insert(l, 0, p); //head ptrlist_insert(l, 1, q); //2nd if (ptrlist_count(l) != 4) return "list size != 4 after inserting four elements"; if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != s || ptrlist_get(l, 4) != NULL) return "got something else out than what was put in"; ptrlist_dispose(l); return NULL; }
const char* /*UNITTEST*/ test_basic(void) { ptrlist_t l = ptrlist_init(); if (ptrlist_count(l) != 0) return "newly allocated list not empty"; ptrlist_dispose(l); return NULL; }
__private_extern__ boolean_t ptrlist_dup(ptrlist_t * dest, ptrlist_t * source) { ptrlist_init(dest); dest->size = dest->count = source->count; if (source->count == 0) return (TRUE); dest->array = malloc(sizeof(*dest->array) * dest->size); if (dest->array == NULL) return (FALSE); bcopy(source->array, dest->array, sizeof(*dest->array) * dest->size); return (TRUE); }
STATIC void parse_nd_options(ptrlist_t * options_p, const char * buf, int len) { int left = len; const struct nd_opt_hdr * opt; int opt_len = 0; const char * scan; ptrlist_init(options_p); for (scan = buf; left >= sizeof(*opt); ) { opt = (struct nd_opt_hdr *)scan; opt_len = opt->nd_opt_len * ND_OPT_ALIGN; if (opt_len > left) { /* truncated packet */ ptrlist_free(options_p); break; } ptrlist_add(options_p, (void *)opt); scan += opt_len; left -= opt_len; } return; }
void jmps_init() { ptrlist_init(&jmps); ptrlist_init(&leveled_jmps); }
void cargo_init(struct cargo *cargo) { memset(cargo, 0, sizeof(*cargo)); ptrlist_init(&cargo->requires); INIT_LIST_HEAD(&cargo->list); }
void dhcpol_init(dhcpol_t * list) { ptrlist_init((ptrlist_t *)list); }
int addconstellation(char* cname) { unsigned long nums, numc, i; char *string; struct sector *fs, *s; struct ptrlist work; double phi; unsigned long r; string = malloc(strlen(cname)+GREEK_LEN+2); if (!string) return -1; ptrlist_init(&work); /* Determine number of sectors in constellation */ nums = mtrandom_uint(GREEK_N); if (nums == 0) nums = 1; mprintf("addconstellation: will create %lu sectors (universe has %lu so far)\n", nums, ptrlist_len(&univ.sectors)); pthread_rwlock_wrlock(&univ.sectornames_lock); fs = NULL; for (numc = 0; numc < nums; numc++) { /* Create a new sector and put it in s */ s = malloc(sizeof(*s)); if (!s) goto err; sprintf(string, "%s %s", greek[numc], cname); if (sector_create(s, string)) goto err; ptrlist_push(&univ.sectors, s); st_add_string(&univ.sectornames, s->name, s); if (fs == NULL) { /* This was the first sector generated for this constellation We need to place this at a suitable point in the universe */ fs = s; if (ptrlist_len(&univ.sectors) == 1) { /* The first constellation always goes in (0, 0) */ if (sector_move(s, 0, 0)) bug("%s", "Error when placing first sector at (0,0)"); } else { /* All others are randomly distributed */ phi = mtrandom_uint(UINT_MAX) / (double)UINT_MAX*2*M_PI; r = 0; i = 2; while (i > 1) { r += mtrandom_ulong(CONSTELLATION_RANDOM_DISTANCE); phi += mtrandom_double(CONSTELLATION_PHI_RANDOM); if (!sector_move(s, POLTOX(phi, r), POLTOY(phi, r))) i = get_neighbouring_systems(NULL, s, CONSTELLATION_MIN_DISTANCE); } } ptrlist_push(&work, s); } else if (ptrlist_len(&work) == 0) { /* This isn't the first sector but no sectors are left in work Put this close to the first sector */ ptrlist_push(&work, s); makeneighbours(fs, s, 0, 0); } else { /* We have sectors in work, put this close to work[0] and add this one to work */ ptrlist_push(&work, s); makeneighbours(ptrlist_entry(&work, 0), s, 0, 0); /* Determine if work[0] has enough neighbours, if so remove it */ if (mtrandom_uint(UINT_MAX) < UINT_MAX/CONSTELLATION_NEIGHBOUR_CHANCE) ptrlist_pull(&work); } mprintf("Created %s (%p) at %ldx%ld\n", s->name, s, s->x, s->y); } pthread_rwlock_unlock(&univ.sectornames_lock); free(string); ptrlist_free(&work); return 0; err: pthread_rwlock_unlock(&univ.sectornames_lock); return -1; }
const char* /*UNITTEST*/ test_remove(void) { ptrlist_t l = ptrlist_init(); int w, x, y, z; int *p = &w, *q = &x, *r = &y, *s = &z; ptrlist_insert(l, 0, s); ptrlist_remove(l, 1); if (ptrlist_count(l) != 1) return "bogus remove actually removed something?"; ptrlist_remove(l, 0); if (ptrlist_count(l) != 0) return "failed to remove single element"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_remove(l, 2); if (ptrlist_count(l) != 2) return "bogus remove actually removed something?"; ptrlist_remove(l, 0); if (ptrlist_count(l) != 1) return "failed to remove head of 2-element list"; ptrlist_remove(l, 0); if (ptrlist_count(l) != 0) return "failed to remove single remaining element"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 0); if (ptrlist_get(l, 0) != q || ptrlist_get(l, 1) != r || ptrlist_get(l, 2) != s || ptrlist_get(l, 3) != NULL) return "remove of element 0 in 4-elem list failed"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 1); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != r || ptrlist_get(l, 2) != s || ptrlist_get(l, 3) != NULL) return "remove of element 1 in 4-elem list failed"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 2); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != s || ptrlist_get(l, 3) != NULL) return "remove of element 2 in 4-elem list failed"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 3); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != NULL) return "remove of element 3 in 4-elem list failed"; ptrlist_clear(l); ptrlist_dispose(l); return NULL; }