int lookup(const char *word) { unsigned char mem[4096]; struct allocator *al = suba_init(mem, 4096, 1, 0); struct varray buf; int m[1024], max, i, wlen = strlen(word), d; varray_init(&buf, sizeof(int), al); for (i = 0; words[i]; i++) { m[i] = strlen(words[i]); } max = i; for (i = 0; i < max; i++) { if ((d = diff(word, 0, wlen, words[i], 0, m[i], NULL, NULL, NULL, DIFF_LIMIT, NULL, NULL, &buf)) == -1) { MMNO(errno); return EXIT_FAILURE; } if (d == 0) { printf("exact match\n"); return 0; } else if (d < DIFF_LIMIT) { printf("%s\n", words[i]); } } return 0; }
void exmem_malloc_init() { // pool zero is EXMEM_RAMDISK on d10 void *mem = _exmem_alloc(0,EXMEM_HEAP_SIZE,0); if(mem) { exmem_heap = suba_init(mem,EXMEM_HEAP_SIZE-EXMEM_HEAP_SKIP,1,1024); } }
kogmo_rtdb_objsize_t kogmo_rtdb_obj_mem_init (kogmo_rtdb_handle_t *db_h) { kogmo_rtdb_objsize_t size = db_h->localdata_p->heap_size; kogmo_rtdb_objsize_t free; void *base = db_h->localdata_p->heap; DBGL(DBGL_DB,"mem_init: %i bytes at %p", size, base); #if defined(RTMALLOC_tlsf) free = init_memory_pool (size, base); #elif defined(RTMALLOC_suba) db_h->heapinfo = suba_init(base, size, 1/*rst*/, 1024/*mincell*/); free = size; #else free = size; #endif db_h->localdata_p->heap_free = free; db_h->localdata_p->heap_used = 0; return free; }
kogmo_rtdb_objsize_t kogmo_rtdb_obj_mem_attach (kogmo_rtdb_handle_t *db_h) { #if !defined(RTMALLOC_tlsf) kogmo_rtdb_objsize_t size = db_h->localdata_p->heap_size; void *base = db_h->localdata_p ->heap; #endif #if defined(RTMALLOC_tlsf) DBGL(DBGL_DB,"mem_attach(tlsf)"); // do nothing #elif defined(RTMALLOC_suba) db_h->heapinfo = suba_init(base, size, 0/*rst*/, 0); DBGL(DBGL_DB,"mem_attach(suba) at %p with %p",base,db_h->heapinfo); if(db_h->heapinfo == NULL) return KOGMO_RTDB_ERR_INVALID; #else DBGL(DBGL_DB,"mem_attach(static)"); // do nothing #endif return 0; }
int AdtInit(int verbose, struct cfg *cfg, char *args[]) { char buf[0xFFFF]; struct allocator *suba; struct stack s; struct linkedlist l; struct varray va; if ((suba = suba_init(buf, 0xFFFF, 1, 0)) == NULL || stack_init(&s, 0, suba) == -1 || linkedlist_init(&l, 0, suba) == -1 || varray_init(&va, sizeof(int), suba) == -1) { AMSG(""); return -1; } linkedlist_add(&l, "two"); stack_push(&s, "two"); linkedlist_add(&l, "three"); stack_push(&s, "one"); varray_get(&va, 444); stack_push(&s, "three"); varray_get(&va, 4); varray_get(&va, 44); linkedlist_add(&l, "one"); if (varray_deinit(&va) != 0 || linkedlist_deinit(&l, NULL, NULL) != 0 || stack_deinit(&s, NULL, NULL) != 0) { AMSG(""); return -1; } tcase_printf(verbose, "done "); cfg = NULL; args[0] = NULL; return 0; }
static void chdk_heap_create(chdk_heap *h, void *mem, int size, int chdk_in_heap, int heap_testing) { if (h) { if (chdk_in_heap) { // If loading CHDK into heap then move heap start past the end of CHDK // and reduce available space by CHDK size (MEMISOSIZE) // round MEMISOSIZE up to next 4 byte boundary if needed (just in case) h->start = mem + ((camera_info.memisosize+3)&0xFFFFFFFC); h->size = size - ((camera_info.memisosize+3)&0xFFFFFFFC); } else { // Set start & size based on requested values h->start = mem; h->size = size; } h->end = h->start + h->size; if (heap_testing) { // For testing exmem allocated memory for corruption from normal camera operations // set the above #define. This will allocate the memory; but won't use it (exmem_heap is set to 0) // Instead all the memory is filled with the guard value below. // In gui_draw_debug_vals_osd (gui.c) the memory is tested for the guard value and if any // corruption has occurred then info about the memory locations that were altered is displayed // Note: do not use heap_testing and chdk_in_heap at the same time unsigned long *p; for (p=(unsigned long*)h->start; p<(unsigned long*)h->end; p++) *p = 0xDEADBEEF; } else { // Normal operation, use the suba allocation system to manage the memory block h->heap = suba_init(h->start,h->size,1,8); } } }
int HashmapExercise(int verbose, struct cfg *cfg, char *args[]) { struct hashmap *h; int rate = 0, i, n = 0; int count = atoi(args[0]); int interval = atoi(args[1]); iter_t riter; iter_t iter; char *str; struct allocator *al = NULL; char *mem; clock_t t0; int chkpnt = 0; struct allocator *hal; if ((mem = malloc(0xFFFFFF)) == NULL || (al = suba_init(mem, 0xFFFFFF, 1, 0)) == NULL || (h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) { AMSG(""); return -1; } hal = AL(h); /* if ((h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) { AMSG(""); return -1; } */ srand(0); t0 = clock(); if (verbose) fprintf(stderr, "\n time count size mem\n"); hashmap_iterate(h, &iter); rate_iterate(&riter); for (i = 0; i < count; i++) { if ((i % interval) == 0) { if (verbose) fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total); rate = rate_next(&riter); } if (rand() % 10 < rate) { str = allocator_alloc(NULL, 8, 0); sprintf(str, "%07d", n++); if (hashmap_put(h, str, str) == -1) { AMSG(""); return -1; } else { /*fputc('+', stderr); */ tcase_printf(verbose, "put %s %d\r", str, hashmap_size(h)); } } else { if (hashmap_is_empty(h)) { /*fputc('0', stderr); */ tcase_printf(verbose, "hashmap empty\r"); } else { str = hashmap_next(h, &iter); if (str) { char *data; tcase_printf(verbose, "get %s %d\r", str, hashmap_size(h)); if (hashmap_remove(h, (void **)&str, (void **)&data) == -1) { AMSG(""); return -1; } /*fputc('-', stderr); */ tcase_printf(verbose, "rem %s %d\r", str, hashmap_size(h)); allocator_free(NULL, str); } else { /*fputc('x', stderr); */ tcase_printf(verbose, "nothing left to iterate over\r"); hashmap_iterate(h, &iter); } } } } if (verbose) fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total); hashmap_del(h, allocator_free, NULL, NULL); /* free(mem); */ if (verbose) fprintf(stderr, "bytes outstanding from allocator: %d\n", hal->alloc_total - hal->free_total); cfg = NULL; return 0; }
int HashmapCount(int verbose, struct cfg *cfg, char *args[]) { struct test t; char *mem = NULL; int ret = 0, fd = 0, data; int useal = atoi(args[0]); struct stat st; void *mm; if (useal) { if ((mem = malloc(0xFFFFFF)) == NULL || (t.al = suba_init(mem, 0xFFFFFF, 1, 0)) == NULL) { AMSG(""); ret = -1; goto err; } } else { t.al = NULL; } if ((t.words = hashmap_new(hash_str, (cmp_fn)strcmp, NULL, t.al)) == NULL || (fd = open(args[1], 0)) == -1 || fstat(fd, &st) == -1) { AMSG(""); ret = -1; goto err; } if ((t.src = mm = mmap(NULL, (int)st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) == NULL) { AMSG(""); ret = -1; goto err; } t.slim = t.src + st.st_size; if (load_words(&t) == -1) { AMSG(""); ret = -1; goto err; } if ((data = (int)hashmap_get(t.words, args[2])) == 0 || atoi(args[3]) != data || (data = (int)hashmap_get(t.words, args[4])) == 0 || atoi(args[5]) != data) { errno = ERANGE; PMNF(errno, ": %d != %d", atoi(args[3]), data); ret = -1; goto err; } if (verbose) print_values(t.words); err: munmap(mm, st.st_size); if (fd) close(fd); hashmap_del(t.words, NULL, NULL, NULL); free(mem); cfg = NULL; return ret; }
int HashmapExercise0(int verbose, struct cfg *cfg, char *args[]) { struct hashmap0 *h; int rate = 0, i, n = 0; int count = atoi(args[0]); int interval = atoi(args[1]); iter_t riter; char *str; clock_t t0; void *mem; struct allocator *al; iter_t iter; int chkpnt = 0; if ((mem = malloc(0xFFFFFFF)) == NULL || (al = suba_init(mem, 0xFFFFFFF, 1, 0)) == NULL || (h = hashmap0_new(HASHMAP_LARGE, hash_str, (compare_fn)strcmp, NULL, allocator_free, NULL, al)) == NULL) { AMSG(""); return -1; } srand(1); fputc('\n', stderr); t0 = clock(); fprintf(stderr, " time count size mem\n"); hashmap0_iterate(h, &iter); rate_iterate(&riter); for (i = 0; i < count; i++) { if ((i % interval) == 0) { fprintf(stderr, "%2d %8ld %8d %8d %8d %d\n", chkpnt++, (clock() - t0) / 1000, hashmap0_size(h), HASHMAP_LARGE, h->al->size_total - h->al->free_total, rate); rate = rate_next(&riter); } if (rand() % 10 < rate) { str = allocator_alloc(NULL, 16, 0); sprintf(str, "%015d", n++); if (hashmap0_put(h, str, str) == -1) { AMSG(""); return -1; } else { /*fputc('+', stderr); */ tcase_printf(verbose, "put %s %d\n", str, hashmap0_size(h)); } } else { if (hashmap0_is_empty(h)) { tcase_printf(verbose, "hashmap0 empty\n"); } else { str = hashmap0_next(h, &iter); if (str) { tcase_printf(verbose, "get %s %d\n", str, hashmap0_size(h)); if ((str = hashmap0_remove(h, str)) == NULL) { errno = EFAULT; PMNO(errno); return -1; } /*fputc('-', stderr); */ tcase_printf(verbose, "remove %s %d\n", str, hashmap0_size(h)); allocator_free(NULL, str); } else { /*fputc('x', stderr); */ tcase_printf(verbose, "nothing left to iterate over\r"); hashmap0_iterate(h, &iter); } } } } fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap0_size(h), HASHMAP_LARGE, h->al->size_total); hashmap0_del(h); tcase_printf(verbose, "hashmap0 done\n"); cfg = NULL; return 0; }