void *_mrealloc (void *memblock, size_t size, const char *file, int line, const char *func) { size_t old_size; if (memblock == NULL) { return _mmalloc (size, file, line, func); } old_size = ( (struct unit_head *) ( (char *) memblock - sizeof (struct unit_head) + sizeof (long)))->size; if (old_size == 0) { old_size = ( (struct unit_head_large *) ( (char *) memblock - sizeof (struct unit_head_large) + sizeof (long)))->size; } if (old_size > size) { // サイズ縮小 -> そのまま返す(手抜き) return memblock; } else { // サイズ拡大 void *p = _mmalloc (size, file, line, func); if (p != NULL) { memcpy (p, memblock, old_size); } _mfree (memblock, file, line, func); return p; } }
int add_service(char proc_name[ADD_STR_LENGTH], int proc_version, char addstr[PROC_NAME_LENGTH], int port) { int i; //the procedure already exists for(i=0 ; i<=SIZE_OF_MAPPER-1 ; i++) { if(Mapper[i].proc_name[0] != '\0' && !strcmp(proc_name, Mapper[i].proc_name) && Mapper[i].proc_version == proc_version) { //proc's new server int server_index = _mmalloc(page_bitmap,MAX_BITMAP_SIZE); Server* new_server = &(seg_heap[server_index]); strcpy(new_server->addstr, addstr); new_server->port = port; new_server->next = -1; //add the new server to the end int index_server_ptr = Mapper[i].server_list; for( ; seg_heap[index_server_ptr].next != -1 ; index_server_ptr = seg_heap[index_server_ptr].next); seg_heap[index_server_ptr].next = server_index; return 0; } } //new procedure for(i=0 ; i<=SIZE_OF_MAPPER-1 ; i++) { if (Mapper[i].proc_name[0] == '\0') { //new mapper item strcpy(Mapper[i].proc_name, proc_name); Mapper[i].proc_version = proc_version; //proc's new server int server_index = _mmalloc(page_bitmap,MAX_BITMAP_SIZE); Server* new_server = &(seg_heap[server_index]); strcpy(new_server->addstr, addstr); new_server->port = port; new_server->next = -1; //add new server Mapper[i].server_list = server_index; //next available server Mapper[i].next_avail_server = server_index; return 0; } } //fail to add return -1; }
char* _mstrdup(const char *p, const char *file, int line, const char *func ) { if(p == NULL) { return NULL; } else { size_t len = strlen(p); char *string = (char *)_mmalloc(len + 1,file,line,func); memcpy(string,p,len+1); return string; } }
void* _mrealloc(void *memblock, size_t size, const char *file, int line, const char *func ) { size_t old_size; if(memblock == NULL) { return _mmalloc(size,file,line,func); } old_size = ((struct unit_head *)((char *)memblock - sizeof(struct unit_head) + sizeof(long)))->size; if( old_size == 0 ) { old_size = ((struct unit_head_large *)((char *)memblock - sizeof(struct unit_head_large) + sizeof(long)))->size; } if(old_size > size) { // Size reduction - return> as it is (negligence) return memblock; } else { // Size Large void *p = _mmalloc(size,file,line,func); if(p != NULL) { memcpy(p,memblock,old_size); } _mfree(memblock,file,line,func); return p; } }
void* _mcalloc(size_t num, size_t size, const char *file, int line, const char *func ) { void *p = _mmalloc(num * size,file,line,func); memset(p,0,num * size); return p; }