void *_Array_Append(void *vmem, void *item, int esize, char *file, int line) { MemHead *mem = (MemHead*)vmem; MemTail *tail; mem--; if (vmem == NULL) { mem = (MemHead*)_Array_New(esize, esize*2, file, line); mem--; mem->used = 0; } if (mem->size - mem->used < mem->esize) { mem = (MemHead*)_Array_Realloc(mem+1, mem->size*2, file, line); mem--; } memcpy(((unsigned char*)(mem+1))+mem->used, item, mem->esize); mem->used += mem->esize; tail = GET_TAIL(mem); tail->used = mem->used; return mem+1; }
int _MEM_size(void *vmem, char *file, int line) { MemHead *mem = (MemHead*)vmem; MemTail *tail; if (!vmem) { fprintf(stderr, "Warning: tried to free NULL pointer!\n"); fprintf(stderr, " at line %d: file: %s\n", line, file); return 0; } mem--; if (mem->code1 == FREECODE) { fprintf(stderr, "MEM_size: Error: Double free!\n"); fprintf(stderr, " at line %d: file: %s\n", line, file); return 0; } if (!_MEM_check(vmem, file, line)) { tail = GET_TAIL(mem); fprintf(stderr, "MEM_size: Error: Corrupted memory block %p!\n", vmem); fprintf(stderr, " at line %d: file: %s\n", line, file); //fprintf(stderr, "[h1=%s, h2=%s, t1=%s, t2=%s] ", mem->code1, mem->code2, tail->code1, tail->code2); return 0; } return mem->size; }
void *_Array_GrowOne(void *vmem, int esize, char *file, int line) { MemHead *mem = (MemHead*)vmem; MemTail *tail; mem--; if (vmem == NULL) { mem = (MemHead*)_Array_New(esize, esize*2, file, line); mem--; mem->used = 0; } if (mem->size - mem->used < mem->esize) { mem = (MemHead*)_Array_Realloc(mem+1, mem->size*2, file, line); mem--; } mem->used += mem->esize; tail = GET_TAIL(mem); tail->used = mem->used; return mem+1; }
ssize_t nidas_circbuf_read_nowait(struct file *filp, char __user* buf, size_t count, struct dsm_sample_circ_buf* cbuf, struct sample_read_state* state) { size_t countreq = count; struct dsm_sample* insamp; size_t bytesLeft = state->bytesLeft; char* samplePtr = state->samplePtr; size_t n; for ( ; count; ) { if ((n = min(bytesLeft,count)) > 0) { if (copy_to_user(buf,samplePtr,n)) return -EFAULT; bytesLeft -= n; count -= n; samplePtr += n; buf += n; if (bytesLeft > 0) break; // user buffer filled samplePtr = 0; INCREMENT_TAIL(*cbuf,cbuf->size); } insamp = GET_TAIL(*cbuf,cbuf->size); if (!insamp) break; // no more samples samplePtr = (char*)insamp; bytesLeft = insamp->length + SIZEOF_DSM_SAMPLE_HEADER; KLOG_DEBUG("bytes left=%zd\n",bytesLeft); } state->samplePtr = samplePtr; state->bytesLeft = bytesLeft; KLOG_DEBUG("read return = %u\n",countreq - count); return countreq - count; }
void _MEM_free(void *vmem, char *file, int line) { MemHead *mem = (MemHead*)vmem; MemTail *tail; pthread_mutex_lock(&mem_mutex); if (!vmem) { fprintf(stderr, "Warning: tried to free NULL pointer!\n"); fprintf(stderr, " at line %d: file: %s\n", line, file); pthread_mutex_unlock(&mem_mutex); return; } mem--; if (mem->code1 == FREECODE) { fprintf(stderr, "Error: Double free!\n"); fprintf(stderr, " at line %d: file: %s\n", line, file); pthread_mutex_unlock(&mem_mutex); return; } if (!_MEM_check(vmem, file, line)) { tail = GET_TAIL(mem); fprintf(stderr, "MEM_free: Error: Corrupted memory block %p!\n", vmem); fprintf(stderr, " at line %d: file: %s\n", line, file); //fprintf(stderr, "[h1=%s, h2=%s, t1=%s, t2=%s] ", mem->code1, mem->code2, tail->code1, tail->code2); pthread_mutex_unlock(&mem_mutex); return; } tail = GET_TAIL(mem); List_Remove(&MemHeads, mem); List_Remove(&MemTails, tail); mem->code1 = mem->code2 = FREECODE; tail->code1 = tail->code2 = FREECODE; free(mem); pthread_mutex_unlock(&mem_mutex); }
void *_Array_Pop(void *vmem, char *file, int line) { MemHead *mem = (MemHead*)vmem; MemTail *tail; mem--; if (mem->used < mem->esize) { fprintf(stderr, "Invalid call to Array_Pop\n"); return NULL; } mem->used -= mem->esize; tail = GET_TAIL(mem); tail->used = mem->used; return (void*)(((unsigned char)(mem+1)) + mem->esize*mem->used); }
int _MEM_check(void *vmem, char *file, int line) { MemHead *mem = (MemHead*) vmem; MemTail *tail; if (!vmem) return 0; mem = (MemHead*) vmem; mem--; if (mem->code1 != H_MEMCODE1) return 0; if (mem->code2 != H_MEMCODE2) return 0; tail = GET_TAIL(mem); if (tail->code1 != T_MEMCODE1) return 0; if (tail->code2 != T_MEMCODE2) return 0; return 1; }
void *_Array_Realloc(void *vmem, size_t newlength, char *file, int line) { MemHead *mem = (MemHead*)vmem, *mem2; MemTail *tail; mem--; mem2 = (MemHead*)_Array_New(mem->esize, newlength, file, line); memcpy(mem2, mem+1, mem->size); mem2--; mem2->used = mem->used; tail = GET_TAIL(mem2); tail->used = mem->used; _MEM_free(mem+1, file, line); return mem2+1; }
/* * Routine that will find the parent of a given object. The * idea is to walk to the end of our siblings and return * our parent. If object is the root then return NIL as parent. */ WORD get_parent(LONG tree, WORD obj) { WORD pobj; if (obj == NIL) return (NIL); else { pobj = GET_NEXT(tree, obj); if (pobj != NIL) { while( GET_TAIL(tree, pobj) != obj ) { obj = pobj; pobj = GET_NEXT(tree, obj); } } return(pobj); } } /* get_parent */
void *_Array_New(size_t esize, size_t length, char *file, int line) { MemHead *mem; MemTail *tail; int size; if (length == 0) return NULL; size = length*esize; size = size == 0 ? esize*4 : size; mem = (MemHead*)_MEM_calloc(size, "array", file, line); mem--; mem->used = length*esize; mem->esize = esize; tail = GET_TAIL(mem); tail->used = length*esize; tail->esize = esize; return mem+1; }
//does not change size unless necassary; if you wish to force //a change in the allocation size, use realloc void *_Array_Resize(void *vmem, int newlen, int esize, char *file, int line) { MemHead *mem = (MemHead*)vmem; MemTail *tail; mem--; if (vmem == NULL) { return _Array_New(esize, newlen, file, line); } if (newlen*mem->esize >= mem->size) { //double buffer mem = (MemHead*)_Array_Realloc(mem+1, newlen*2, file, line); mem--; } mem->used = newlen*mem->esize; tail = GET_TAIL(mem); tail->used = mem->used; return mem+1; }
void MEM_PrintMemBlocks(FILE *file) { MemHead *mem; pthread_mutex_lock(&mem_mutex); if (MemHeads.first == NULL) { pthread_mutex_unlock(&mem_mutex); return; } fprintf(file, "=====Allocated Memory Blocks:======\n"); for (mem=(MemHead*)MemHeads.first; mem; mem=mem->next) { MemTail *tail = GET_TAIL(mem); if (!_MEM_check(mem+1, __FILE__, __LINE__)) { fprintf(file, "[corrupted, h1=%s, h2=%s, t1=%s, t2=%s] ", mem->code1, mem->code2, tail->code1, tail->code2); } fprintf(file, "%s:%d: size: %d\n", memprint_truncate(mem->file), mem->line, mem->size); } pthread_mutex_unlock(&mem_mutex); }
static VALUE reduce0(VALUE val, VALUE data, VALUE self) { struct cparse_params *v; VALUE reduce_to, reduce_len, method_id; long len; ID mid; VALUE tmp, tmp_t = Qundef, tmp_v = Qundef; long i, k1, k2; VALUE goto_state; Data_Get_Struct(data, struct cparse_params, v); reduce_len = RARRAY_PTR(v->reduce_table)[v->ruleno]; reduce_to = RARRAY_PTR(v->reduce_table)[v->ruleno+1]; method_id = RARRAY_PTR(v->reduce_table)[v->ruleno+2]; len = NUM2LONG(reduce_len); mid = value_to_id(method_id); /* call action */ if (len == 0) { tmp = Qnil; if (mid != id_noreduce) tmp_v = rb_ary_new(); if (v->debug) tmp_t = rb_ary_new(); } else { if (mid != id_noreduce) { tmp_v = GET_TAIL(v->vstack, len); tmp = RARRAY_PTR(tmp_v)[0]; } else { tmp = RARRAY_PTR(v->vstack)[ RARRAY_LEN(v->vstack) - len ]; } CUT_TAIL(v->vstack, len); if (v->debug) { tmp_t = GET_TAIL(v->tstack, len); CUT_TAIL(v->tstack, len); } CUT_TAIL(v->state, len); } if (mid != id_noreduce) { if (v->use_result_var) { tmp = rb_funcall(v->parser, mid, 3, tmp_v, v->vstack, tmp); } else { tmp = rb_funcall(v->parser, mid, 2, tmp_v, v->vstack); } } /* then push result */ PUSH(v->vstack, tmp); if (v->debug) { PUSH(v->tstack, reduce_to); rb_funcall(v->parser, id_d_reduce, 4, tmp_t, reduce_to, v->tstack, v->vstack); } /* calculate transition state */ if (RARRAY_LEN(v->state) == 0) rb_raise(RaccBug, "state stack unexpectedly empty"); k2 = num_to_long(LAST_I(v->state)); k1 = num_to_long(reduce_to) - v->nt_base; D_printf("(goto) k1=%ld\n", k1); D_printf("(goto) k2=%ld\n", k2); tmp = AREF(v->goto_pointer, k1); if (NIL_P(tmp)) goto notfound; i = NUM2LONG(tmp) + k2; D_printf("(goto) i=%ld\n", i); if (i < 0) goto notfound; goto_state = AREF(v->goto_table, i); if (NIL_P(goto_state)) { D_puts("(goto) table[i] == nil"); goto notfound; } D_printf("(goto) table[i]=%ld (goto_state)\n", NUM2LONG(goto_state)); tmp = AREF(v->goto_check, i); if (NIL_P(tmp)) { D_puts("(goto) check[i] == nil"); goto notfound; } if (tmp != LONG2NUM(k1)) { D_puts("(goto) check[i] != table[i]"); goto notfound; } D_printf("(goto) check[i]=%ld\n", NUM2LONG(tmp)); D_puts("(goto) found"); transit: PUSH(v->state, goto_state); v->curstate = NUM2LONG(goto_state); return INT2FIX(0); notfound: D_puts("(goto) not found: use default"); /* overwrite `goto-state' by default value */ goto_state = AREF(v->goto_default, k1); goto transit; }