void request(const int rtype) { /* room_users_list is not handled */ if (is_logged() && rtype >= USERS_LIST && rtype <= ROOM_USERS_LIST ) { if (allocate_mem(REQUEST, &request_data)) { request_data->type = REQUEST; request_data->request_type = rtype; strcpy(request_data->user_name, username); if (send_message(request_data->type, request_data) != FAIL) { int mtype = (rtype == USERS_LIST) ? USERS : (rtype == ROOMS_LIST) ? ROOMS : ROOM_USERS; if (allocate_mem(mtype, &request_response_data)) { if (wait_until_received(mtype) != FAIL) { display_request_result(); } else { writestr("Request wasn't proceeded."); } free_mem(request_response_data); } else { writestr("Couldn't allocate data structure."); } } free_mem(request_data); } else { writestr("Couldn't allocate request_data structure."); } } else { writestr("To perform this, you have to be logged in."); } }
void join(char * str) { if (!is_inroom() && is_logged()) { if (allocate_mem(ROOM, &room_data)) { room_data->type = ROOM; room_data->operation_type = ENTER_ROOM; strcpy(room_data->user_name, username); strcpy(room_data->room_name, str); if (send_message(room_data->type, room_data) != FAIL && wait_until_received(RESPONSE) != FAIL) { if (response_data.response_type == ENTERED_ROOM_SUCCESS) { char buf[50]; inroom = 1; strcpy(roomname, room_data->room_name); sprintf(buf, "%s: %s", "You've successfully entered room", roomname); writestr(buf); } else { writestr("Couldn't enter room."); } } else { writestr("Server didn't answer."); } free_mem(room_data); } } else if (is_inroom() && is_logged()) { if (strcmp(str, roomname)) { /* User didn't choose the same room to log in again */ if (allocate_mem(ROOM, &room_data)) { room_data->type = ROOM; room_data->operation_type = CHANGE_ROOM; strcpy(room_data->user_name, username); strcpy(room_data->room_name, str); if (send_message(room_data->type, room_data) != FAIL && wait_until_received(RESPONSE) != FAIL) { if (response_data.response_type == CHANGE_ROOM_SUCCESS) { char buf[50]; strcpy(roomname, room_data->room_name); sprintf(buf, "%s: %s", "You've successfully changed room for", roomname); writestr(buf); } else { writestr("Couldn't change room."); } } else { writestr("Server didn't answer"); } free_mem(room_data); } else { writestr("Couldn't allocate room_data structure."); } } else { writestr("No use in entering the same room again."); } } else { writestr("To perform it, you have to be logged in."); } }
struct tp * tp_create(int max_jobs) { struct tp *pool = allocate_mem(sizeof(*pool)); pool->jobs = allocate_mem(sizeof(struct tp_entry)*max_jobs); pool->capacity = max_jobs; pool->size = 0; pthread_mutex_init(&pool->mutex, 0); return pool; }
/*创建新的进程,主要是获取内存的申请数量*/ int new_process(){ struct allocated_block *ab; int size; int ret; ab=(struct allocated_block *)malloc(sizeof(struct allocated_block)); if(!ab) exit(-5); ab->next = NULL; pid++; sprintf(ab->process_name, "PROCESS-%02d", pid); ab->pid = pid; printf("Memory for %s:", ab->process_name); scanf("%d", &size); if(size>0) ab->size=size; ret = allocate_mem(ab); /* 从空闲区分配内存,ret==1表示分配ok*/ /*如果此时allocated_block_head尚未赋值,则赋值*/ if((ret==1) &&(allocated_block_head == NULL)){ allocated_block_head=ab; return 1; } /*分配成功,将该已分配块的描述插入已分配链表*/ else if (ret==1) { ab->next=allocated_block_head; allocated_block_head=ab; return 2; }else if(ret==-1){ /*分配不成功*/ printf("Allocation fail\n");// free(ab); return -1; } return 3; }
void leave() { if (is_inroom()) { if (allocate_mem(ROOM, &room_data)) { room_data->type = ROOM; room_data->operation_type = LEAVE_ROOM; strcpy(room_data->user_name, username); strcpy(room_data->room_name, roomname); if (send_message(room_data->type, room_data) != FAIL && wait_until_received(RESPONSE) != FAIL) { inroom = 0; if (response_data.response_type == LEAVE_ROOM_SUCCESS) { writestr("Successfully left room"); } else { writestr("Some error occurred."); } } free_mem(room_data); } else { writestr("Couldn't allocate room_data structure."); } } else { writestr("To perform it, you have to be in room."); } }
char * read_stdin(int *stdin_size) { int input_size; char *temp = NULL; #ifdef ARRAYFILE input_size = fasta_output_len; #else struct stat stat; fstat(0, &stat); input_size = stat.st_size; #endif char *result = allocate_mem(input_size); do { assert(fgets_unlocked(result, input_size, stdin)); } while (strncmp(result, ">THREE", 6)); int read = 0; while (fgets_unlocked(result + read, input_size - read, stdin)) { int len = strlen(result + read); if (len == 0 || result[read] == '>') { break; } read += len; if (result[read - 1] == '\n') { read--; } } result[read++] = '>'; //result = realloc(result, read); temp = allocate_mem(read); if(!temp) { return NULL; } memcpy(temp,result, read); nv_free(result); result = temp; *stdin_size = read; return result; }
void* kma_malloc(kma_size_t size) { if(size > PAGESIZE) return NULL; if(page_entry == NULL) init_page_entry(); return allocate_mem(size); }
struct ht_node * ht_values_as_vector(struct ht_ht *ht) { struct ht_node *v = allocate_mem(ht->items*sizeof(struct ht_node)); struct ht_node *n = ht_first(ht); for (int i = 0; i < ht->items; i++) { v[i] = *n; n = ht_next(ht); } return v; }
//check consistency of input dimension and call allocation routine, seed for the rng is provided (s) int hypercube_lowd::set_up(int dim_in, int s) { if (dim_in>0 && (unsigned int)dim_in <= 8*sizeof(int)) { if (HC_VERBOSE) cerr<<"hypercube_lowd::set_up(): setting up...!"; dim=dim_in; mem=false; if (s==0) seed=time(NULL); else seed=s; if (HC_VERBOSE) cerr<<"done.\n"; return allocate_mem(); } else { cerr <<"hypercube_lowd: got: "<<dim_in<<", need positive and <32 dimension!\n"; return HC_BADARG; } }
CIYuv::CIYuv(int h, int w, int chroma_format) { height = h; width = w; sampling = chroma_format; Y = U = V = NULL; comp[0] = comp[1] = comp[2] = NULL; pBuffer = NULL; FuncFilter = NULL; switch(sampling) { case 400: // padding chroma components heightUV = height/2; widthUV = width/2; break; case 420: heightUV = height/2; widthUV = width/2; break; case 422: heightUV = height; widthUV = width/2; break; case 444: heightUV = height; widthUV = width; break; default: fprintf(stderr, "Unknown chroma sampling\n"); height = width = sampling = 0; Y = U = V = NULL; comp[0] = comp[1] = comp[2] = NULL; return; } picsizeY = height*width; picsizeUV = heightUV*widthUV; size_in_byte = picsizeY + picsizeUV*2; if(!allocate_mem()) { height = width = sampling = 0; Y = U = V = NULL; comp[0] = comp[1] = comp[2] = NULL; pBuffer = NULL; } }
char *arrayfile_join2 (FILE *pFile) { long lSize; char * buffer; size_t result; struct rqst_struct rqst; lSize = 10125; rqst.bytes = lSize; rqst.pid = PROC_ID; rqst.id = CHUNK_ID+2; buffer = pnvmalloc(rqst.bytes, &rqst); if(!buffer) { fprintf(stderr,"arrayfile_join2: outfile allocation failed \n"); return; } return buffer; // allocate memory to contain the whole file: buffer = (char*) malloc (sizeof(char)*lSize); if (buffer == NULL) { fputs ("Memory error",stderr); exit (2); } // copy the file into the buffer: buffer = (char *)allocate_mem(lSize); if (buffer == NULL) { fputs ("Reading error",stderr); exit (3); } /* the whole file is now loaded in the memory buffer. */ return buffer; return 0; }
static void test_alloc_overlap(void) { /* Test i#1175: create some +rx DGC. Then change it to +rw via mmap * instead of mprotect and ensure DR catches a subsequent code modification. * We allocate two pages and put the code one page in so that our * modifying mmap can have its prot match the region base's prot to * make it harder for DR to detect. */ char *buf = allocate_mem(PAGE_SIZE*2, ALLOW_READ|ALLOW_WRITE|ALLOW_EXEC); char *code = copy_to_buf(buf + PAGE_SIZE, PAGE_SIZE, NULL, CODE_INC, COPY_NORMAL); protect_mem(code, PAGE_SIZE, ALLOW_READ|ALLOW_EXEC); test_print(code, 42); #ifdef UNIX code = mmap(buf, PAGE_SIZE*2, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON|MAP_FIXED, 0, 0); #else code = VirtualAlloc(buf, PAGE_SIZE*2, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #endif code = copy_to_buf(code, PAGE_SIZE, NULL, CODE_DEC, COPY_NORMAL); test_print(code, 42); /* Hmm, there is no free_mem()... */ }
bool CIYuv::Resize(int h, int w, int chroma_format) { height = h; width = w; sampling = chroma_format; switch(sampling) { case 400: // padding chroma components heightUV = height/2; widthUV = width/2; break; case 420: heightUV = height/2; widthUV = width/2; break; case 422: heightUV = height; widthUV = width/2; break; case 444: heightUV = height; widthUV = width; break; default: fprintf(stderr, "Unknown chroma sampling\n"); Y = U = V = NULL; return false; } picsizeY = height*width; picsizeUV = heightUV*widthUV; size_in_byte = picsizeY + picsizeUV*2; return allocate_mem(); }
/* Reduced from V8, which uses x64 absolute addresses in code which ends up * being sandboxed. The original code is not self-modifying, but is flushed * enough to trigger sandboxing. * * 0x000034b7f8b11366: 48 a1 48 33 51 36 ff 7e 00 00 movabs 0x7eff36513348,%rax * ... * 0x000034b7f8b11311: 48 a3 20 13 51 36 ff 7e 00 00 movabs %rax,0x7eff36511320 */ void test_mov_abs(void) { char *rwx_mem = allocate_mem(4096, ALLOW_READ|ALLOW_WRITE|ALLOW_EXEC); char *pc = rwx_mem; void *(*do_selfmod_abs)(void); void *out_val = 0; uint64 *global_addr = (uint64 *) pc; /* Put a 64-bit 0xdeadbeefdeadbeef into mapped memory. Typically most * memory from mmap is outside the low 4 GB, so this makes sure that any * mangling we do avoids address truncation. */ *(uint64 *)pc = 0xdeadbeefdeadbeefULL; pc += 8; /* Encode an absolute load and store. If we write it in assembly, gas picks * the wrong encoding, so we manually encode it here. It has to be on the * same page as the data to trigger sandboxing. */ do_selfmod_abs = (void *(*)(void))pc; *pc++ = 0x48; /* REX.W */ *pc++ = 0xa1; /* movabs load -> rax */ *(uint64 **)pc = global_addr; pc += 8; *pc++ = 0x48; /* REX.W */ *pc++ = 0xa3; /* movabs store <- rax */ *(uint64 **)pc = global_addr; pc += 8; *pc++ = 0xc3; /* ret */ print("before do_selfmod_abs\n"); out_val = do_selfmod_abs(); print(PFX"\n", out_val); /* rwx_mem is leaked, tools.h doesn't give us a way to free it. */ }
int fannkuchredux(int n) { int *perm; int perm1[n]; int count[n]; int maxFlipsCount = 0; int permCount = 0; int checksum = 0; int i; perm = (int *)allocate_mem(n); for (i=0; i<n; i+=1) perm1[i] = i; int r = n; while (1) { while (r != 1) { count[r-1] = r; r -= 1; } for (i=0; i<n; i+=1) perm[i] = perm1[i]; int flipsCount = 0; int k; while ( !((k = perm[0]) == 0) ) { int k2 = (k+1) >> 1; for (i=0; i<k2; i++) { int temp = perm[i]; perm[i] = perm[k-i]; perm[k-i] = temp; } flipsCount += 1; } maxFlipsCount = max(maxFlipsCount, flipsCount); checksum += permCount % 2 == 0 ? flipsCount : -flipsCount; /* Use incremental change to generate another permutation */ while (1) { if (r == n) { if (n == 10 && checksum != ref_checksum) return -1; if (n == 10 && maxFlipsCount != ref_flips) return -1; return 0; } int perm0 = perm1[0]; i = 0; while (i < r) { int j = i + 1; perm1[i] = perm1[j]; i = j; } perm1[r] = perm0; count[r] = count[r] - 1; if (count[r] > 0) break; r++; } permCount++; } }
void run() { long time, delta; int ret, cli_idx, cli; nfds_t nfds; if (allocate_mem() < 0) { free_mem(); return; } cli_cnt = 0; nfds = 2; fd_all[0].fd = pcap_fd; fd_all[0].events = POLLIN; fd_all[1].fd = listenfd; fd_all[1].events = POLLIN; for (cli = 0; cli < max_client; ++cli) avai_no[cli] = max_client - cli - 1; avai_cnt = max_client; result.item[max_query - 1].next = -1; for (ret = max_query - 2; ret >= 0; --ret) result.item[ret].next = ret + 1; result.avai = 0; waiting.item[max_query - 1].next = -1; for (ret = max_query - 2; ret >= 0; --ret) waiting.item[ret].next = ret + 1; waiting.avai = 0; waiting.head = -1; for (cli = 0; cli < max_client; ++cli) { in_buffer[cli].len = 0; in_buffer[cli].newline = in_buffer[cli].buf; out_buffer[cli].head = 0; out_buffer[cli].tail = 0; out_buffer[cli].tot = 0; pending[cli].in = -1; pending[cli].out = -1; } fprintf(stderr, "max_client: %d, max_query: %d. Exceeded will be rejected.\n", max_client, max_query); time = -1; while (running) { pop_waiting(); if (time == -1) delta = 1000; else delta = time - gettime(); while (delta >= 0) { for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) { cli = cli_no[cli_idx]; push_waiting(cli); if ((fd_cli[cli_idx].events & POLLIN) == 0 && in_buffer[cli].len != GUK_MAX_QUERY_LEN) fd_cli[cli_idx].events |= POLLIN; } if ((ret = poll(fd_all, nfds, delta + 1)) > 0) { if (fd_all[0].revents == POLLIN) gk_cm_read_cap(); ret = (fd_all[1].revents == POLLIN); // ret here stand for new connection available nfds -= 2; for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) { if (fd_cli[cli_idx].revents & (POLLERR | POLLNVAL | POLLHUP)) { fprintf(stderr, "Connection closed or broken."); close_client(cli_idx); --nfds; continue; } cli = cli_no[cli_idx]; if (fd_cli[cli_idx].revents & POLLOUT) { do { pop_result(cli); } while (try_write(cli)); if (all_written(cli)) fd_cli[cli_idx_of[cli]].events &= ~POLLOUT; last_act[cli] = gettime(); } if (fd_cli[cli_idx].revents & POLLIN) { while (try_read(cli)) { push_waiting(cli); } if (in_buffer[cli].len == GUK_MAX_QUERY_LEN) fd_cli[cli_idx].events &= ~POLLIN; last_act[cli] = gettime(); } else if (ret && is_idle(cli) && gettime() - last_act[cli] >= (GUK_SERV_TIMEOUT * 1000)) { fprintf(stderr, "Client timeout. "); close_client(cli_idx); --nfds; } } /* remove closed clients */ for (cli_idx = 0; cli_cnt > (int)nfds; ) { while (cli_idx < (int)nfds && fd_cli[cli_idx].fd >= 0) ++cli_idx; if (cli_idx == (int)nfds) { cli_cnt = cli_idx; break; } else { while (fd_cli[--cli_cnt].fd < 0); memcpy(fd_cli + cli_idx, fd_cli + cli_cnt, sizeof(struct pollfd)); cli_idx_of[(cli_no[cli_idx] = cli_no[cli_cnt])] = cli_idx; } } nfds += 2; if (ret) while (cli_cnt < max_client && accept_client() == 0) ++nfds; } else if (ret < 0) perror("poll"); time = gk_cm_conn_next_time(); if (time == -1) delta = 1000; else delta = time - gettime(); } time = gk_cm_conn_step(); } gk_cm_finalize(); for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) { cli = cli_no[cli_idx]; do { pop_result(cli); } while (try_write(cli)); } for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) close_client(cli_idx); free_mem(); }
int main(int argc, char *argv[]) { double res = 0.; int i; #ifndef X64 int j; #endif char *buf; #ifdef LINUX stack_t sigstack; #endif #ifdef USE_DYNAMO dynamorio_app_init(); dynamorio_app_start(); #endif #ifdef LINUX /* our modrm16 tests clobber esp so we need an alternate stack */ sigstack.ss_sp = (char *) malloc(ALT_STACK_SIZE); sigstack.ss_size = ALT_STACK_SIZE; sigstack.ss_flags = SS_ONSTACK; i = sigaltstack(&sigstack, NULL); assert(i == 0); intercept_signal(SIGILL, signal_handler); intercept_signal(SIGSEGV, signal_handler); #else SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER) our_top_handler); #endif buf = allocate_mem(7*256+1, ALLOW_READ|ALLOW_WRITE|ALLOW_EXEC); assert(buf != NULL); #ifndef X64 print("Jumping to a sequence of every addr16 modrm byte\n"); for (j=0; j<256; j++) { int mod = ((j >> 6) & 0x3); /* top 2 bits */ int reg = ((j >> 3) & 0x7); /* middle 3 bits */ int rm = (j & 0x7); /* bottom 3 bits */ # if defined(LINUX) || defined(X64) buf[j*7 + 0] = 0x65; /* gs: */ # else buf[j*7 + 0] = 0x64; /* fs: */ # endif buf[j*7 + 1] = 0x67; /* addr16 */ buf[j*7 + 2] = 0x8b; /* load */ # ifdef WINDOWS /* Windows can't handle stack pointer being off */ if (reg == 4) { /* xsp */ buf[j*7 + 3] = j | 0x8; } else buf[j*7 + 3] = j; /* nearly every single modrm byte */ # else buf[j*7 + 3] = j; /* every single modrm byte */ # endif if (mod == 1) { buf[j*7 + 4] = 0x03; /* disp */ buf[j*7 + 5] = 0xc3; } else if (mod == 2 || (mod == 0 && rm == 6)) { buf[j*7 + 4] = 0x03; /* disp */ buf[j*7 + 5] = 0x00; /* disp */ } else { buf[j*7 + 4] = 0xc3; /* ret */ buf[j*7 + 5] = 0xc3; } buf[j*7 + 6] = 0xc3; } buf[256*7] = 0xcc; print_access_vio = false; for (j=0; j<256; j++) { i = setjmp(mark); if (i == 0) test_modrm16(&buf[j*7]); else continue; } print("Done with modrm test: tested %d\n", j); count = 0; print_access_vio = true; #endif /* !X64 */ /* multi-byte nop tests (case 9862) */ i = setjmp(mark); if (i == 0) { print("Testing nops\n"); test_nops(); print("Done with nops\n"); } /* SSE3 and 3DNow instrs will not run on all processors so we can't have this * regression test fully test everything: for now its main use is running * manually on the proper machines, or manually verifying decoding of these, * but we'll leave as a suite/ regression test. */ /* SSE3 tests: mostly w/ modrm of (%edx) */ i = setjmp(mark); if (i == 0) { print("Testing SSE3\n"); test_sse3(buf); print("Should not get here\n"); } /* 3D-Now tests: mostly w/ modrm of (%ebx) */ i = setjmp(mark); if (i == 0) { print("Testing 3D-Now\n"); test_3dnow(buf); print("Should not get here\n"); } /* case 6962: far call/jmp tests * Note that DR currently gets the target address wrong for all of these * since we skip the segment and only care about the address: * not going to fix that anytime soon. */ print("Testing far call/jmp\n"); test_far_cti(); /* PR 242815: data16 mbr */ print("Testing data16 mbr\n"); test_data16_mbr(); #ifdef LINUX free(sigstack.ss_sp); #endif print("All done\n"); #ifdef USE_DYNAMO dynamorio_app_stop(); dynamorio_app_exit(); #endif return 0; }