void* debug_realloc(const char* fname, int line, void* mem, size_t size) { void* new_mem; new_mem = debug_malloc(fname, line, size); if(MC_ERR(new_mem == NULL)) return NULL; /* Copy contents from the old memory chunk */ if(mem != NULL) { mem_info_t* mi; mi = mem_hashtable[MEM_HASHTABLE_INDEX(mem)]; while(mi->mem != mem) { if(MC_ERR(mi == NULL)) { /* Not registered? */ MC_TRACE("%s:%d: \tdebug_realloc(%p): Attempting to realloc " "non-allocated memory.", fname, line, mem); MC_ASSERT(1 == 0); } mi = mi->next; } memcpy(new_mem, mem, MC_MIN(size, mi->size)); debug_free(fname, line, mem); } return new_mem; }
static struct wbuf * wbuf_add(struct socket * sock, unsigned sz) { struct wbuf * wbuf; struct wbuf_chain * wc = (struct wbuf_chain *)sock->buf; assert(wc); wbuf = debug_malloc(sizeof(struct wbuf) + sz); if (!wbuf) return NULL; wbuf->len = sz; wbuf->written = wbuf->unacked = 0; wbuf->next = NULL; if (wc->head == NULL) wc->head = wc->tail = wbuf; else { wc->tail->next = wbuf; wc->tail = wbuf; } sock->buf_size += sz; debug_tcp_print("buffer %p size %d\n", wbuf, sock->buf_size); return wbuf; }
/** * Create new connection * @param sock * @return XS_CONN * */ XS_CONN *conn_new(int sock) { XS_CONN *conn; debug_malloc(conn, sizeof(XS_CONN), XS_CONN); if (conn == NULL) { log_error("not enough memory to create connection (SOCK:%d)", sock); close(sock); return NULL; } else { int val = 1; // set socket option setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &val, sizeof(val)); fcntl(sock, F_SETFL, O_NONBLOCK); // put to event list memset(conn, 0, sizeof(XS_CONN)); conn->tv.tv_sec = CONN_TIMEOUT; event_set(&conn->ev, sock, EV_READ, client_ev_cb, conn); CONN_EVENT_ADD(); log_debug_conn("add connection to event base (CONN:%p, SOCK:%d)", conn, sock); conn_server.num_burst++; return conn; } }
int set_argv(t_env *env) { size_t pos; size_t tmp_pos; size_t len; char **ptr; ptr = env->argv; env->error = NO_ERROR; pos = env->start; while (pos < env->len && env->interprete[pos] != DELIMITER) { while (pos < env->len && env->interprete[pos] == SPACING) ++pos; tmp_pos = pos; if ((len = should_len(env, &pos, &ptr))) { if (!(*ptr = (char *)debug_malloc(sizeof(char) * (len + 1)))) return (ERROR); if (env->argv_pool_size < _POSIX_ARG_MAX) env->argv_pool[env->argv_pool_size++] = *ptr; else env->error = ERROR; extract_content(env, tmp_pos, *ptr); ++ptr; } else --env->argc; if (env->error == ERROR) return (ERROR); } *ptr = NULL; env->start = pos + 1; return (NO_ERROR); }
void * debug_malloc0(const char *where, int line, const char *func, int size) { void *ret = debug_malloc(where, line, func, size); if (ret) memset(ret, 0, size); return ret; }
/** * Save the current zcmd into cmds list * @param conn * @return CMD_RES_CONT/CMD_RES_NOMEM */ static int conn_zcmd_save(XS_CONN *conn) { XS_CMDS *cmds; // change RETURN value to CONT log_debug_conn("save command into CMDS (CMD:%d)", conn->zcmd->cmd); debug_malloc(cmds, sizeof(XS_CMDS), XS_CMDS); if (cmds == NULL) { log_error_conn("failed to allocate memory for CMDS (SIZE:%d)", sizeof(XS_CMDS)); return CMD_RES_NOMEM; } cmds->next = NULL; if (conn->flag & CONN_FLAG_ZMALLOC) { // use zcmd directly conn->flag ^= CONN_FLAG_ZMALLOC; cmds->cmd = conn->zcmd; } else { // copy zcmd debug_malloc(cmds->cmd, XS_CMD_SIZE(conn->zcmd), XS_CMD); if (cmds->cmd != NULL) memcpy(cmds->cmd, conn->zcmd, XS_CMD_SIZE(conn->zcmd)); else { log_error_conn("failed to allocate memory for CMDS->cmd (CMD:%d, SIZE:%d)", conn->zcmd->cmd, XS_CMD_SIZE(conn->zcmd)); debug_free(cmds); return CMD_RES_NOMEM; } } // add the cmd to chain of cmds if (conn->zhead == NULL) conn->ztail = conn->zhead = cmds; else { conn->ztail->next = cmds; conn->ztail = cmds; } return CMD_RES_CONT; }
void * debug_realloc(const char *where, int line, const char *func, void *ptr, int size) { void *ret = debug_malloc(where, line, func, size); if (ret && ptr) memcpy(ret, ptr, size); debug_free(where, line, func, ptr); return ret; }
void* debug_calloc(int n, int s, const char *fn, int lno, char *tstamp) #endif { void *p = debug_malloc(n * s, fn, lno, tstamp); if (p) memset(p, 0, n*s); return p; }
void * debug_calloc(const char *file, unsigned line, const char *function, size_t count, size_t size ) { void *ptr = debug_malloc( file, line, function, count * size ); if( ptr ) memset( ptr, 0, count * size ); return ptr; }
void *debug_calloc(size_t n, size_t size, char *pcFile, int iLine) { void *pBlock; size *= n; pBlock = debug_malloc( size, pcFile, iLine ); if ( pBlock != NULL ) bzero( pBlock, size ); return pBlock; }
static void* checked_malloc(size_t size) { void* address = debug_malloc(size); if (address == NULL) { parse_exception("out of memory for command execution", -1); return NULL; } return address; }
void driver_up(const char * label, endpoint_t ep) { struct nic * nic; nic = lookup_nic_by_drv_name(label); if (nic) { debug_print("LWIP : driver '%s' / %d is up for /dev/%s\n", label, ep, nic->name); nic->drv_ep = ep; } else { printf("LWIP : WARNING unexpected driver '%s' up event\n", label); return; } nic->state = DRV_IDLE; /* * FIXME * * We set the initial ip to 0.0.0.0 to make dhcpd broadcasing work * at the very begining. dhcp should use raw socket but it is a little * tricy in the current dhcp implementation */ if (!netif_add(&nic->netif, (ip_addr_t *) &ip_addr_any, &ip_addr_none, &ip_addr_none, nic, ethernetif_init, ethernet_input)) { printf("LWIP : failed to add device /dev/%s\n", nic->name); nic->drv_ep = NONE; } if (nic->is_default) netif_set_default(&nic->netif); /* FIXME we support ethernet only, 2048 is safe */ nic->tx_buffer = debug_malloc(2048); if (nic->tx_buffer == NULL) panic("Cannot allocate tx_buffer"); /* When driver restarts, the rx_pbuf is likely ready to receive data * from its previous instance. We free the buffer here, nobody depends * on it. A new one is allocated when we send a new read request to the * driver. */ if (nic->rx_pbuf) { pbuf_free(nic->rx_pbuf); nic->rx_pbuf = NULL; } /* prepare the RX grant once and forever */ if (cpf_setgrant_direct(nic->rx_iogrant, nic->drv_ep, (vir_bytes) &nic->rx_iovec, 1 * sizeof(iovec_s_t), CPF_READ) != OK) panic("Failed to set grant"); }
void *debug_xmalloc(const char *file, int line, int size) { void *newp = debug_malloc(file, line, size); if (!newp) { fprintf(stderr, "No memory!\n"); abort(); } return newp; }
void* debug_strdup(const char *s, const char *fn, int lno, char *tstamp) #endif { char *cp; if (!s) return NULL; cp = (char*) debug_malloc(strlen(s)+1, fn, lno, tstamp); if (cp) strcpy(cp, s); return cp; }
char *debug_strdup(const char *src, char *pcFile, int iLine) { char *dst; if ( src == NULL ) return NULL; dst = debug_malloc( strlen( src ) + 1, pcFile, iLine ); if ( dst != NULL ) strcpy( dst, src ); return dst; }
char * debug_strdup(const char *where, int line, const char *func, const char *ptr) { int size; char *ret; if (!ptr) return NULL; size = strlen(ptr) + 1; ret = debug_malloc(where, line, func, size); memcpy(ret, ptr, size); return ret; }
static gpointer debug_realloc (gpointer mem, gsize n_bytes) { #if 0 AllocationHeader *header = ((AllocationHeader*)mem) - 1; guint old_size; assert (memcmp (header->underrun_detection_magic, underrun_detection_magic, 4) == 0); assert (memcmp ((char*)(header + 1) + header->size, overrun_detection_magic, 4) == 0); assert (header->context->n_bytes_used >= header->size); old_size = header->size; header = realloc (header, sizeof (AllocationHeader) + n_bytes + 4); header->size = n_bytes; memcpy ((char*)(header + 1) + n_bytes, overrun_detection_magic, 4); header->context->n_bytes_used -= old_size; header->context->n_bytes_used += n_bytes; return header + 1; #else void *rv; guint size; if (mem) { AllocationHeader *header = ((AllocationHeader*)mem) - 1; assert (memcmp (header->underrun_detection_magic, underrun_detection_magic, 4) == 0); size = header->size; assert (memcmp ((char*)(header + 1) + size, overrun_detection_magic, 4) == 0); assert (header->context->n_bytes_used >= size); } else size = 0; if (log_fd >= 0) { log_uint (LOG_MAGIC_REALLOC); log_pointer (mem); log_uint (size); } stack_levels_to_ignore++; rv = debug_malloc (n_bytes); memcpy (rv, mem, MIN (n_bytes, size)); debug_free (mem); stack_levels_to_ignore--; return rv; #endif }
void *debug_realloc(void *old_blk, size_t size, char *pcFile, int iLine) { size_t old_size; void *pBlock = debug_malloc( size, pcFile, iLine ); old_size = old_blk == NULL ? 0 : *(((size_t *)old_blk) - 1); if ( pBlock != NULL && old_blk != NULL ) memcpy( pBlock, old_blk, min( old_size, size ) ); if ( ( size == 0 || pBlock != NULL ) && ( old_blk != NULL ) ) debug_free( old_blk ); return pBlock; }
void * _Malloc( char *file, int line, size_t s ) { void *mem; mem = debug_malloc(file, line, s); if (mem == NULL) { md_perror(""); md_exit(NULL, 1); } return (mem); }
struct ccnl_buf_s* debug_buf_new(void *data, int len, const char *fn, int lno, char *tstamp) #endif { struct ccnl_buf_s *b = (struct ccnl_buf_s *) debug_malloc(sizeof(*b) + len, fn, lno, tstamp); if (!b) return NULL; b->next = NULL; b->datalen = len; if (data) memcpy(b->data, data, len); return b; }
void * _Realloc( char *file, int line, void *p, size_t s ) { if (p == NULL) p = debug_malloc(file, line, s); else p = debug_realloc(file, line, p, s); if (p == NULL) { md_perror(""); md_exit(NULL, 1); } return (p); }
static void print_stack_trace(struct tracing_stack_trace* stackTrace, const Print& print) { if (stackTrace == NULL || stackTrace->depth <= 0) return; static const size_t kBufferSize = 256; char* buffer = (char*)debug_malloc(kBufferSize); for (int32 i = 0; i < stackTrace->depth; i++) { addr_t address = stackTrace->return_addresses[i]; const char* symbol; const char* demangledName = NULL; const char* imageName; bool exactMatch; addr_t baseAddress; if (elf_debug_lookup_symbol_address(address, &baseAddress, &symbol, &imageName, &exactMatch) == B_OK) { if (buffer != NULL) { bool isObjectMethod; demangledName = debug_demangle_symbol(symbol, buffer, kBufferSize, &isObjectMethod); } print(" %p %s + 0x%lx (%s)%s\n", (void*)address, demangledName != NULL ? demangledName : symbol, address - baseAddress, imageName, exactMatch ? "" : " (nearest)"); } else print(" %p\n", (void*)address); } if (buffer != NULL) debug_free(buffer); }
/// the new array operator void* operator new[] (size_t size, const char* file, const int line) { void* p = debug_malloc(size, file, line); return p; }
void * debug_realloc(const char *file, unsigned line, const char *function, void *old_ptr, size_t old_size, size_t new_size ) { struct debug_memory_header *old_hdr, *new_hdr; struct debug_memory_footer *old_ftr, *new_ftr; void *new_ptr; if(!old_ptr) return debug_malloc( file, line, function, new_size ); if(!new_size) { debug_free( file, line, function, old_ptr ); return NULL; } old_hdr = header_from_data(old_ptr); if(old_hdr->magic != DEBUG_MEMORY_MAGIC) { debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n", file, line, function, old_ptr); debug_assert(0); return NULL; } old_ftr = footer_from_header(old_hdr); if(old_ftr->magic != DEBUG_MEMORY_MAGIC) { debug_printf("%s:%u:%s: buffer overflow %p\n", old_hdr->file, old_hdr->line, old_hdr->function, old_ptr); debug_assert(0); } /* alloc new */ new_hdr = os_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr)); if(!new_hdr) { debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n", file, line, function, (long unsigned)new_size); return NULL; } new_hdr->no = old_hdr->no; new_hdr->file = old_hdr->file; new_hdr->line = old_hdr->line; new_hdr->function = old_hdr->function; new_hdr->size = new_size; new_hdr->magic = DEBUG_MEMORY_MAGIC; new_hdr->tag = 0; #if DEBUG_FREED_MEMORY new_hdr->freed = FALSE; #endif new_ftr = footer_from_header(new_hdr); new_ftr->magic = DEBUG_MEMORY_MAGIC; pipe_mutex_lock(list_mutex); LIST_REPLACE(&old_hdr->head, &new_hdr->head); pipe_mutex_unlock(list_mutex); /* copy data */ new_ptr = data_from_header(new_hdr); memcpy( new_ptr, old_ptr, old_size < new_size ? old_size : new_size ); /* free old */ old_hdr->magic = 0; old_ftr->magic = 0; os_free(old_hdr); return new_ptr; }
void* operator new[] (size_t size, const char* file, const int line) { void* p = debug_malloc(size, file, line); debug_new_log("new[](%zu, %s, %d) %p\n", size, file, line, p); return p; }
void *debug_malloc_ex(unsigned int size) { return debug_malloc(size, "<none>", 0); }
/** * Parse incoming data & execute commands * Called after data received. * @return CMD_RES_xxx (PAUSE|CONT|...quits...) */ int conn_cmds_parse(XS_CONN *conn, zcmd_exec_t func) { int off = 0, rc = CMD_RES_CONT; // check zcmd if (conn->zcmd != NULL) { if (conn->zcmd_left > 0 && conn->rcv_size > 0) { char *buf = XS_CMD_BUFTAIL(conn->zcmd) - conn->zcmd_left; off = (conn->zcmd_left > conn->rcv_size ? conn->rcv_size : conn->zcmd_left); memcpy(buf, conn->rcv_buf, off); log_debug_conn("copy rcv_buf to zcmd (SIZE:%d, RCV_SIZE:%d, ZCMD_LEFT:%d)", off, conn->rcv_size, conn->zcmd_left); conn->zcmd_left -= off; } // execute zcmd (otherwise: rcv_size - off <= 0) if (conn->zcmd_left == 0) rc = conn_zcmd_exec(conn, func); } // parse the cmd from rcv_buf while (rc == CMD_RES_CONT && (conn->rcv_size - off) >= sizeof(XS_CMD)) { conn->zcmd = (XS_CMD *) (conn->rcv_buf + off); off += sizeof(XS_CMD); log_debug_conn("get command {cmd:%d,arg1:%d,arg2:%d,blen1:%d,blen:%d}", conn->zcmd->cmd, conn->zcmd->arg1, conn->zcmd->arg2, conn->zcmd->blen1, conn->zcmd->blen); // check the zcmd is full or not if ((XS_CMD_BUFSIZE(conn->zcmd) + off) > conn->rcv_size) { XS_CMD *cmd; debug_malloc(cmd, XS_CMD_SIZE(conn->zcmd), XS_CMD); if (cmd == NULL) { log_error_conn("failed to allocate memory for ZCMD (CMD:%d, SIZE:%d)", conn->zcmd->cmd, XS_CMD_SIZE(conn->zcmd)); conn->zcmd = NULL; off -= sizeof(XS_CMD); // reset offset to cmd header rc = CMD_RES_NOMEM; } else { memcpy(cmd, conn->zcmd, conn->rcv_size - off + sizeof(XS_CMD)); conn->zcmd_left = XS_CMD_BUFSIZE(conn->zcmd) - (conn->rcv_size - off); conn->zcmd = cmd; conn->flag |= CONN_FLAG_ZMALLOC; // current zcmd must be free off = conn->rcv_size; log_debug_conn("wait left data of zcmd (CMD:%d, ZCMD_LEFT:%d)", cmd->cmd, conn->zcmd_left); } break; } // execute the zcmd (on rcv_buffer) off += XS_CMD_BUFSIZE(conn->zcmd); rc = conn_zcmd_exec(conn, func); } // flush the send buffer if (CONN_FLUSH() != 0) rc = CMD_RES_IOERR; // move the buffer conn->rcv_size -= off; if (conn->rcv_size > 0 && off > 0) memcpy(conn->rcv_buf, conn->rcv_buf + off, conn->rcv_size); // return the rc return rc; }