static ssize_t block_ce_count_show(struct kobject *kobj, struct attribute *attr, char *data) { struct edac_device_block *block = to_block(kobj); return sprintf(data, "%u\n", block->counters.ce_count); }
bool dmalloc_init() { /* Two choices: * 1. Append prologue and epilogue blocks to the start and the end of the freelist * 2. Initialize freelist pointers to NULL * * Note: We provide the code for 2. Using 1 will help you to tackle the * corner cases succinctly. */ is_init = true; size_t max_bytes = ALIGN(MAX_HEAP_SIZE); int i = 0; for (i = 0; i < ARRAY_SIZE; i++) { freelist_arr[i] = NULL; } metadata_t *fl = (metadata_t*) sbrk(max_bytes); // returns heap_region, which is initialized to freelist heap_begin = fl; /* Q: Why casting is used? i.e., why (void*)-1? */ if (fl == (void *)-1) return false; //prologue block footer_t* prologue = (footer_t *)fl; set_size((metadata_t *)prologue, 0); set_alloc((metadata_t *)prologue); //epilogue block metadata_t* epilogue = (metadata_t *)((void *)fl + max_bytes - META_SIZE); set_size(epilogue, 0); set_alloc(epilogue); //freelist fl = (metadata_t *)((void *)fl + FOOTER_SIZE); set_size(fl, max_bytes - 2 * META_SIZE - 2 * FOOTER_SIZE); set_free(fl); set_size((metadata_t *)to_footer(to_block(fl)), meta_size(fl)); set_free((metadata_t *)to_footer(to_block(fl))); fl->next = NULL; fl->prev = NULL; add_node(fl); return true; }
metadata_t * extend_heap(size_t space) { int numchunk = (space + META_SIZE + FOOTER_SIZE) / CHUNK_SIZE; if ((space + META_SIZE + FOOTER_SIZE) % CHUNK_SIZE > 0) { numchunk += 1; } size_t space_a = numchunk * CHUNK_SIZE; void *last_brk = sbrk(space_a); if (errno == ENOMEM) { printf("no room for extending heap\n"); return NULL; } metadata_t *epilogue = to_meta(last_brk + space_a); epilogue->prev = NULL; epilogue->next = NULL; set_alloc(epilogue); metadata_t *start = to_meta(last_brk); start->prev = NULL; start->next = NULL; set_free(start); set_size(start, space_a - META_SIZE - FOOTER_SIZE); footer_t *end = to_footer(to_block(start)); set_free((metadata_t *)end); set_size((metadata_t *)end, space_a - META_SIZE - FOOTER_SIZE); //add_node(start); bool left_f = is_free((metadata_t *)((void *)start - FOOTER_SIZE)); if (left_f) { //process the linked list //change the size of two blocks void *ptr = to_block(start); delete_node(to_meta(left_block(ptr))); int left_size = block_size(left_block(ptr)); int new_size = left_size + FOOTER_SIZE + META_SIZE + block_size(ptr); set_size(to_meta(left_block(ptr)), new_size); set_size((metadata_t *)to_footer(ptr), new_size); set_free(to_meta(left_block(ptr))); set_free((metadata_t *)to_footer(ptr)); start = to_meta(left_block(ptr)); // add_node(to_meta(left_block(ptr))); } add_node(start); return start; }
static void edac_device_ctrl_block_release(struct kobject *kobj) { struct edac_device_block *block; debugf1("%s()\n", __func__); block = to_block(kobj); kobject_put(&block->instance->ctl->kobj); }
/* DEVICE block kobject release() function */ static void edac_device_ctrl_block_release(struct kobject *kobj) { struct edac_device_block *block; debugf1("%s()\n", __func__); /* get the container of the kobj */ block = to_block(kobj); /* map from 'block kobj' to 'block->instance->controller->main_kobj' * now 'release' the block kobject */ kobject_put(&block->instance->ctl->kobj); }
void* dmalloc(size_t numbytes, int flag) { if(!is_init) { //Initialize through sbrk call first time if(!dmalloc_init()) { return NULL; } } assert(numbytes > 0); /* Your code goes here */ size_t space = ALIGN(numbytes); // go over the linked list, find the first fit //metadata_t* flpt = freelist; metadata_t* flpt = find_fit(space, flag); if (!flpt) { //return NULL; if (!(flpt = extend_heap(space))) { printf("extend heap failed! \n"); return NULL; } } size_t rest = meta_size(flpt) - space; delete_node(flpt); if (rest < ALIGN(META_SIZE + FOOTER_SIZE + 1)) { set_alloc(flpt); set_alloc((metadata_t *)(to_footer(to_block(flpt)))); } else { rest = rest - META_SIZE - FOOTER_SIZE; set_size(flpt, space); set_alloc(flpt); set_size((metadata_t *)(to_footer(to_block(flpt))), space); set_alloc((metadata_t *)(to_footer(to_block(flpt)))); metadata_t* newmt = (metadata_t *)((void *)flpt + META_SIZE + space + FOOTER_SIZE); set_size(newmt, rest); set_free(newmt); set_size((metadata_t *)to_footer(to_block(newmt)), rest); set_free((metadata_t *)to_footer(to_block(newmt))); add_node(newmt); } void * result = to_block(flpt); assert(result != NULL); if (!result) { int a = 1; } return result; }
typename br::value_at_key<Map,bf::pair<Key1,Key2>>::type::Matrix& at_h(Map& h, const ttt::Indice<Key1>& indice, const ttt::Indice<bf::pair<Key2,Key1>>& isparse) { return to_block(bf::at_key<bf::pair<Key1,Key2>>(h),indice,isparse); }
typename br::value_at_key<Map,bf::pair<Key,Key>>::type::Matrix& at_h(Map& h, const ttt::Indice<Key>& indice) { return to_block(bf::at_key<bf::pair<Key,Key>>(h),indice,0); }