void NNLayer::free_space () { rm_free(weight, n_neuron); rm_free(wbackup, n_neuron); rm_free(wdelta, n_neuron); rm_free(wdel_old, n_neuron); rm_free(wdir, n_neuron); rv_free(sig_der); n_input = n_neuron = 0; }
void Matrix::freeBuffer () { if (x != NULL) { assert(size > 0); rm_free(x, size); } init(); }
static void *coalesce(void *bp) { size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); size_t size = GET_SIZE(HDRP(bp)); if(prev_alloc && next_alloc) //101 { add_free(bp); //return bp; } else if(prev_alloc && !next_alloc) //100 { rm_free(NEXT_BLKP(bp)); size += GET_SIZE(HDRP(NEXT_BLKP(bp))); PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); add_free(bp); } else if(!prev_alloc && next_alloc) //001 { size += GET_SIZE(HDRP(PREV_BLKP(bp))); PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); bp = PREV_BLKP(bp); } else //000 { rm_free(NEXT_BLKP(bp)); size += GET_SIZE(HDRP(NEXT_BLKP(bp))) + GET_SIZE(HDRP(PREV_BLKP(bp))); PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0)); bp = PREV_BLKP(bp); } return bp; }
/* * mm_realloc - Implemented simply in terms of mm_malloc and mm_free */ void *mm_realloc(void *ptr, size_t size) { size_t oldsize = GET_SIZE(HDRP(ptr)); size_t asize; void *newptr; if(ptr == NULL) return mm_malloc(size); else if(size == 0) { mm_free(ptr); return NULL; } if(size <= DSIZE) asize = 2 * DSIZE; else asize = DSIZE * ((size + (DSIZE) + (DSIZE - 1)) / DSIZE); if(asize <= oldsize) { //ptr = replace(ptr, asize); return ptr; } else //next block { size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(ptr))); size_t next_size = GET_SIZE(HDRP(NEXT_BLKP(ptr))); if(!next_alloc && (oldsize + next_size) >= asize) { rm_free(NEXT_BLKP(ptr)); PUT(HDRP(ptr), PACK(oldsize + next_size, 1)); PUT(FTRP(ptr), PACK(oldsize + next_size, 1)); return ptr; } /*else if(next_size == 0) { if(extend_heap(CHUNKSIZE / WSIZE) == NULL) return 0; next_size = GET_SIZE(HDRP(NEXT_BLKP(ptr))); rm_free(NEXT_BLKP(ptr)); PUT(HDRP(ptr), PACK(oldsize + next_size, 1)); PUT(FTRP(ptr), PACK(oldsize + next_size, 1)); return replace(ptr, asize); }*/ else { newptr = mm_malloc(size); memcpy(newptr, ptr, oldsize); mm_free(ptr); return newptr; } } }
void *TagIndex_RdbLoad(RedisModuleIO *rdb, int encver) { unsigned long long elems = RedisModule_LoadUnsigned(rdb); TagIndex *idx = NewTagIndex(); while (elems--) { size_t slen; char *s = RedisModule_LoadStringBuffer(rdb, &slen); InvertedIndex *inv = InvertedIndex_RdbLoad(rdb, INVERTED_INDEX_ENCVER); assert(inv != NULL); TrieMap_Add(idx->values, s, MIN(slen, MAX_TAG_LEN), inv, NULL); rm_free(s); } return idx; }
static void *place(void *bp, size_t asize) { //place it in the back size_t csize = GET_SIZE(HDRP(bp)); if((csize - asize) >= (2 * DSIZE)) //split and alloc { PUT(HDRP(bp), PACK((csize - asize), 0)); PUT(FTRP(bp), PACK((csize - asize), 0)); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); return bp; } else //only alloc and remove { rm_free(bp); PUT(HDRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1)); return bp; } }
void TagIndex_Free(void *p) { TagIndex *idx = p; TrieMap_Free(idx->values, InvertedIndex_Free); rm_free(idx); }
int main() { size_t shadow_rec_size = 32; size_t shadow_wordsize = 4; int* prot_array; int* unprot_array; double prot_write_times; double prot_read_times; double unprot_write_times; double unprot_read_times; struct timeval tvBeginProtWrite, tvEndProtWrite; struct timeval tvBeginProtRead, tvEndProtRead; struct timeval tvBeginUnprotWrite, tvEndUnprotWrite; struct timeval tvBeginUnprotRead, tvEndUnprotRead; int i, value; printf("MAIN - start\n"); /*initializes rm*/ if (rm_init(read_handler, write_handler, NULL, NULL, shadow_rec_size, shadow_wordsize) == -1) { printf("main: rm_init error!!\n"); return -1; } printf("N is %d\n", N); /*allocates protected mem*/ prot_array = (int*)rm_malloc(N * sizeof(int)); if(prot_array == NULL) printf("[main] ERROR: Could not allocate protected mem!\n"); /*allocates unprotected mem*/ unprot_array = (int*)malloc(N * sizeof(int)); if(unprot_array == NULL) printf("[main] ERROR: Could not allocate unprotected mem!\n"); /* PROTECTED WRITE LOOP */ printf("Protected write loop\n"); /*fetches initial time*/ gettimeofday(&tvBeginProtWrite, NULL); /*performs N writes to prot mem*/ for(i=0; i<N; i++) { /*performs access*/ *(prot_array + i) = i; } /*fetches final time*/ gettimeofday(&tvEndProtWrite, NULL); prot_write_times = tvEndProtWrite.tv_sec + (tvEndProtWrite.tv_usec*0.000001) -tvBeginProtWrite.tv_sec - (tvBeginProtWrite.tv_usec*0.000001); /* UNPROTECTED WRITE LOOP */ printf("Unprotected write loop\n"); /*fetches initial time*/ gettimeofday(&tvBeginUnprotWrite, NULL); /*performs N writes to unprot mem*/ for(i=0; i<N; i++) { /*performs access*/ *(unprot_array + i) = i; } /*fetches final time*/ gettimeofday(&tvEndUnprotWrite, NULL); unprot_write_times = tvEndUnprotWrite.tv_sec + (tvEndUnprotWrite.tv_usec*0.000001) -tvBeginUnprotWrite.tv_sec - (tvBeginUnprotWrite.tv_usec*0.000001); /* PROTECTED READ LOOP */ printf("Protected read loop\n"); /*fetches initial time*/ gettimeofday(&tvBeginProtRead, NULL); /*performs N reads from prot mem*/ for(i=0; i<N; i++) { /*performs access*/ value = *(prot_array + i); } /*fetches final time*/ gettimeofday(&tvEndProtRead, NULL); prot_read_times = tvEndProtRead.tv_sec + (tvEndProtRead.tv_usec*0.000001) -tvBeginProtRead.tv_sec - (tvBeginProtRead.tv_usec*0.000001); /* UNPROTECTED READ LOOP */ printf("Unprotected read loop\n"); /*fetches initial time*/ gettimeofday(&tvBeginUnprotRead, NULL); /*performs N reads from unprot mem*/ for(i=0; i<N; i++) { /*performs access*/ value = *(unprot_array + i); } /*fetches final time*/ gettimeofday(&tvEndUnprotRead, NULL); unprot_read_times = tvEndUnprotRead.tv_sec + (tvEndUnprotRead.tv_usec*0.000001) -tvBeginUnprotRead.tv_sec - (tvBeginUnprotRead.tv_usec*0.000001); /*prints debug controls*/ printf("\n*** DEBUG ***\n"); printf("write_num = %d\n", write_num); printf("read_num = %d\n", read_num); printf("*(prot_array) = %d\n", *(prot_array)); printf("*(prot_array + 5) = %d\n", *(prot_array + 5)); printf("*(prot_array + N - 1) = %d\n", *(prot_array + N - 1)); /*prints results*/ printf("\n*** RESULTS ***\n"); printf("Seconds taken for %d protected writes: %f\n", N, prot_write_times); printf("Seconds taken for %d unprotected writes: %f\n", N, unprot_write_times); printf("Seconds taken for %d protected reads: %f\n", N, prot_read_times); printf("Seconds taken for %d unprotected reads: %f\n", N, unprot_read_times); printf("Ratio between prot and unprot writes: %f\n", prot_write_times/unprot_write_times); printf("Ratio between prot and unprot reads: %f\n", prot_read_times/unprot_read_times); /*clean up*/ rm_free(prot_array); free(unprot_array); printf("MAIN - end\n"); return 0; }