int vmspace_init(vmspace_t *vms, uintptr_t addr, uintptr_t sz) { /* FIXME: Assert starts and finishes on a page boundary! */ range_t r; r.start = addr; r.extent = sz; vms->start = addr; vms->size = sz; spinlock_init(&vms->lock); size_t overhead = round_to_page_size(buddy_calc_overhead(r)); size_t npages = overhead >> get_page_shift(); uintptr_t start = r.start + r.extent - overhead; int ok = map(start, alloc_pages(PAGE_REQ_NONE, npages), npages, PAGE_WRITE); assert(ok == 0 && "map() failed in vmspace_init!"); r.extent -= overhead; buddy_init(&vms->allocator, (uint8_t*)start, r, /*start_freed=*/0); buddy_free_range(&vms->allocator, r); return 0; }
int meminit(long n_bytes, unsigned int flags, int parm1, int parm2) { // Buddy scheme if (flags == 0x1) { int handle = buddy_init(n_bytes, parm1); return (handle); } else { return (0); } }
/** * This adds a zone to the kernel memory pool. Zones exist to allow there to be * multiple non-adjacent regions of physically contiguous memory. The * bookkeeping needed to cover the gaps would waste a lot of memory and have no * benefit. * * Arguments: * [IN] base_addr: Base address of the memory pool. * [IN] size: Size of the memory pool in bytes. * * NOTE: Currently only one zone is supported. Calling kmem_create_zone() more * than once will result in a panic. */ void kmem_create_zone(unsigned long base_addr, size_t size) { unsigned long pool_order = ilog2(roundup_pow_of_two(size)); unsigned long min_order = MIN_ORDER; /* For now, protect against calling kmem_create_zone() more than once */ BUG_ON(kmem != NULL); /* Initialize the underlying buddy allocator */ if ((kmem = buddy_init(base_addr, pool_order, min_order)) == NULL) panic("buddy_init() failed."); }
int main(int argc, char** argv) { int opt; status_t prog_status; in = stdin; // Parse command line options while ((opt = getopt(argc, argv, "i:")) != -1) { switch (opt) { case 'i': in = fopen(optarg, "r"); break; case '?': switch (optopt) { case 'i': fprintf(stderr, "ERROR: Missing filename after '%c'", optopt); return EXIT_FAILURE; } print_usage(argv[0], stdout); return EXIT_FAILURE; } } // Error check the input file if (in == NULL) { perror("ERROR: Failed to open input file."); return EXIT_FAILURE; } // Zero memory memset(var_map, 0, sizeof(var_map)); // Execute program buddy_init(); prog_status = parse_file(); if (in != stdin) fclose(in); if (prog_status == SUCCESS) return EXIT_SUCCESS; else return EXIT_FAILURE; }
void main() { /* init OS wrapper */ heap_size=0xffff - (word)&heap; /* calc heap size */ register_interfaces(); yx=(yx_t *)query_interface("yx"); /* init buddy */ buddy_init(); /* main loop */ while (TRUE) { buddy_harvest_events(); message_dispatch(); } }
int meminit(long n_bytes, unsigned int flags, int parm1, int* parm2){ int rv; mp.count = 0; if((flags & 0x1)==0x1){ printf("0x1"); rv = buddy_init(n_bytes, parm1, mp); } else if((flags & 0x08)==0x08){printf("0x8\n");} else if ((flags & 0x10)==0x10){printf("0x10\n");} else if ((flags & 0x20)==0x20){printf("0x20\n");} else if ((flags & 0x40)==0x40){printf("0x40\n");} else{ printf("Invalid bits set: %#010x\n", flags); } return rv; }
int main() { setbuf(stdout, NULL); memarea ma; unsigned int sizepow2 = 25; /* 32 Mb */ blockinfo *blocks = malloc(buddy_nblocks(sizepow2) * sizeof(blockinfo)); char *membase = malloc(1 << sizepow2); memset(membase, 0, 1 << sizepow2); buddy_init(&ma, sizepow2, membase, blocks); srand(1); int iterations = 40000; void *allocated[iterations]; int got = 0; int malloc_probability = 50; int i; for (i = 0; i < iterations; i++) { if (((rand() % 100) < malloc_probability) || (0 == got)) { int nbytes = rand() % (2 * 1024 * 1024); allocated[got] = buddy_alloc(&ma, nbytes); if (NULL != allocated[got]) got++; } else { int index = rand() % got; buddy_free(&ma, allocated[index]); memmove(&allocated[index], &allocated[index + 1], (got - index - 1) * sizeof(void *)); got--; } print_mem_line(&ma, 128); if (0 < DELAYMS) { struct timespec st; st.tv_sec = DELAYMS / 1000; st.tv_nsec = (DELAYMS % 1000) * 1000000; nanosleep(&st, NULL); } } return 0; }
/* Module entry point */ static int __init init_mod(void) { int ret; /* register ourselves as a character device */ ret = register_chrdev(MAJOR_NUM, DEVICE_NAME, &file_ops); if (ret < 0) { printk(KERN_INFO "vmm: could not register the device (error: %d)\n", ret); return ret; } /* initialize the buddy pool and tree */ ret = buddy_init(pool_size); if (ret < 0) { printk(KERN_INFO "vmm: could not allocate buddy pool\n"); return -ENOMEM; } printk(KERN_INFO "vmm: Module loaded successfully (device number: %d, pool size: %d)\n", MAJOR_NUM, pool_size); return 0; }
Eina_Bool eina_mempool_init(void) { char *path; _eina_mempool_log_dom = eina_log_domain_register("eina_mempool", EINA_LOG_COLOR_DEFAULT); if (_eina_mempool_log_dom < 0) { EINA_LOG_ERR("Could not register log domain: eina_mempool"); return 0; } EINA_ERROR_NOT_MEMPOOL_MODULE = eina_error_msg_static_register( EINA_ERROR_NOT_MEMPOOL_MODULE_STR); _backends = eina_hash_string_superfast_new(NULL); /* dynamic backends */ _modules = eina_module_arch_list_get(NULL, PACKAGE_LIB_DIR "/eina/modules/mp", MODULE_ARCH); path = eina_module_environment_path_get("HOME", "/.eina/mp/modules/mp"); _modules = eina_module_arch_list_get(_modules, path, MODULE_ARCH); if (path) free(path); path = eina_module_environment_path_get("EINA_MODULES_MEMPOOL_DIR", "/eina/modules/mp"); _modules = eina_module_arch_list_get(_modules, path, MODULE_ARCH); if (path) free(path); path = eina_module_symbol_path_get((const void *)eina_init, "/eina/modules/mp"); _modules = eina_module_arch_list_get(_modules, path, MODULE_ARCH); if (path) free(path); if (!_modules) { ERR("no mempool modules able to be loaded."); eina_hash_free(_backends); goto mempool_init_error; } eina_module_list_load(_modules); /* builtin backends */ #ifdef EINA_STATIC_BUILD_BUDDY buddy_init(); #endif #ifdef EINA_STATIC_BUILD_CHAINED_POOL chained_init(); #endif #ifdef EINA_STATIC_BUILD_EMEMOA_FIXED ememoa_fixed_init(); #endif #ifdef EINA_STATIC_BUILD_EMEMOA_UNKNOWN ememoa_unknown_init(); #endif #ifdef EINA_STATIC_BUILD_FIXED_BITMAP fixed_bitmap_init(); #endif #ifdef EINA_STATIC_BUILD_ONE_BIG one_big_init(); #endif #ifdef EINA_STATIC_BUILD_PASS_THROUGH pass_through_init(); #endif return EINA_TRUE; mempool_init_error: eina_log_domain_unregister(_eina_mempool_log_dom); _eina_mempool_log_dom = -1; return EINA_FALSE; }
int main(int argc, char *argv[]) { int i; char ch; int count; unsigned long int seed; size_t size; struct element x[MAX_ITEMS]; int loc; if (argc < 2) { fprintf(stderr, "Usage: %s <num of tests> [random seed] [silent|terse|verbose|interactive]\n", argv[0]); exit(1); } count = atol(argv[1]); if (argc == 3) { seed = atol(argv[2]); srandom(seed); } if (argc == 4) { if (argv[3][0] == 's') { verbosity = SILENT; } else if (argv[3][0] == 't') { verbosity = TERSE; } else if (argv[3][0] == 'v') { verbosity = VERBOSE; } else if (argv[3][0] == 'i') { verbosity = INTERACTIVE; setvbuf(stdin, NULL, _IONBF, 0); } } if (verbosity > TERSE) system("clear"); buddy_init(0); if (verbosity > TERSE) { printf("Buddy system lists after initialization.\n"); printBuddyLists(); } if (verbosity == INTERACTIVE) { ch = getchar(); system("clear"); if (ch == 'q') exit(0); } for (i =0; i < MAX_ITEMS; i++) { x[i].ptr = NULL; x[i].size = 0; } for (i=0; i < count; i++) { //printf("GL "); loc = (i) % MAX_ITEMS; // where to put in our table //printf("Trying(%d): ", i); if (x[loc].ptr) { // printf("loc=%d ptr=%p size=%lu", loc, x[loc].ptr, x[loc].size); buddy_free(x[loc].ptr); // printf(" Done"); if (verbosity > SILENT) printf("buddy_freed address %p of size %lu in x[%d]\n", x[loc].ptr, x[loc].size, loc); if (verbosity > TERSE) printBuddyLists(); x[loc].ptr = NULL; x[loc].size = 0; // printf(" IfComplete "); } else { size = random() % MAX_REQUEST + 1; // how big a request // printf("loc=%d ptr=%p size=%lu", loc, x[loc].ptr, size); x[loc].ptr = (char *) buddy_malloc(size*sizeof(char)); // printf(" Done"); if (x[loc].ptr == NULL) { perror("TestBuddy:"); exit(1); } x[loc].size = size*sizeof(char); memset(x[loc].ptr, '1', x[loc].size); if (verbosity > SILENT) printf("buddy_malloced %lu bytes and stored address %p at x[%d]\n", size*sizeof(char), x[loc].ptr, loc); if (verbosity > TERSE) printBuddyLists(); // printf(" IfComplete "); } //printf ("V"); if (verbosity == INTERACTIVE) { ch = getchar(); system("clear"); if (ch == 'q') exit(0); } //printf("F\n"); } #ifdef STATS printBuddySystemStats(); #endif exit(0); }