int main(){ my_initialize_heap(5000); /*printf("TEST 1\n"); int *first =(int*) my_alloc(sizeof(int)); printf("%p\n",first); my_free(first); int *second =(int*) my_alloc(sizeof(int)); printf("%p\n",second); my_free(second);*/ printf("TEST 2\n"); int* one = (int*)my_alloc(sizeof(int)); printf("%d\n", one); int* two = (int*)my_alloc(sizeof(int)); printf("%d\n",two ); /* printf("TEST 3\n"); my_initialize_heap(50); int *uno =(int*) my_alloc(sizeof(int)); printf("%p\t\n",uno); int *dos = (int*)my_alloc(sizeof(int)); printf("%p\t\n",dos); int *tres = (int*)my_alloc(sizeof(int)); printf("%p\t\n",tres); my_free(dos); double *d_tres = (double*)my_alloc(sizeof(double)); printf("\nNew Allocation\n"); printf("%p\t\n",d_tres); int *tres_new = (int*)my_alloc(sizeof(int)); printf("%p\t\n", tres_new);*/ /*printf("TEST 4\n"); my_initialize_heap(50); char *word = (char*)my_alloc(sizeof(unsigned char*)); printf("%p\t\n",word); int *numero = (int*)my_alloc(sizeof(int)); printf("%p\n",numero*/ /*printf("TEST 5\n"); int* array = (int*)my_alloc((100 * sizeof(int))); int* int_val = (int*)my_alloc(sizeof(int)); printf("array: %p\t",array); printf("int value: %p\t\n",int_val); my_free(array); printf("Array has been freed\n"); printf("array: %p\t",array); printf("int value: %p\t\n",int_val);*/ //standard_deviation(); return 0; }
int main() { double_32 *a = my_alloc(32*32); int counter = 1; for (int i = 0; i < 32; i++) { for (size_t j = 0; j < 32; j++) { a[j+i*32] = counter++; } } double_32 *b = my_alloc(32*32); for (int i = 0; i < 32; i++) { for (size_t j = 0; j < 32; j++) { b[j+i*32] = 10; } } double_32 *c = my_alloc(32*32); /* Call the assembler function */ asmMul32(a, b, c); for (int i = 0; i < 32; i++) { for (size_t j = 0; j < 32; j++) { printf("%f ", c[j+i*32]); } printf("\n"); } free(a); free(b); free(c); return 0; }
void test_5() { my_initialize_heap(3000); int* arr = (int*)my_alloc((100 * sizeof(int))); int* i = (int*)my_alloc(sizeof(int)); printf("address: %p\taddress: %p\n", arr, i); my_free(arr); printf("address: %p\taddress: %p\n", arr, i); system("pause"); }
void test_4() { my_initialize_heap(300); printf("-----Test Case 4-----\n"); char *a = (char*)my_alloc(sizeof(unsigned char*)); printf("char a: %p\t\n", a); int *b = (int*)my_alloc(sizeof(int)); printf("int a: %p\t\n", b); system("pause"); }
void test_1() { my_initialize_heap(300); printf("-----Test Case 1-----\n"); int *x = (int*)my_alloc(sizeof(int)); printf("%p\t\n",x); my_free(x); int *y = (int*)my_alloc(sizeof(int)); printf("%p\n", y); my_free(y); system("pause"); }
void read_type_form(FILE *fp) { CELL *cell1, *cell2; int i, j; LineNo = 1; i = 1; while (! s_feof(fp)) { LineNoForError = LineNo; cell1 = s_read(fp); Type[i].name = (U_CHAR *) my_alloc((sizeof(U_CHAR)*strlen(_Atom(car(cell1)))) + 1); strcpy(Type[i].name, _Atom(car(cell1))); cell1 = car(cdr(cell1)); j = 1; while (!Null(cell2 = car(cell1))) { Form[i][j].name = (U_CHAR *) my_alloc((sizeof(U_CHAR) * strlen(_Atom(car(cell2)))) + 1); strcpy(Form[i][j].name, _Atom(car(cell2))); Form[i][j].gobi = (U_CHAR *) my_alloc((sizeof(U_CHAR) * strlen(_Atom(car(cdr(cell2))))) + 1); if (strcmp(_Atom(car(cdr(cell2))), "*") == 0) strcpy(Form[i][j].gobi, ""); else strcpy(Form[i][j].gobi, _Atom(car(cdr(cell2)))); if (!Null(car(cdr(cdr(cell2))))) { /* 語尾の表記に漢字が混ざっている場合 */ Form[i][j].gobi_yomi = (U_CHAR *) my_alloc((sizeof(U_CHAR) * strlen(_Atom(car(cdr(cdr(cell2)))))) + 1); if (strcmp(_Atom(car(cdr(cdr(cell2)))), "*") == 0) strcpy(Form[i][j].gobi_yomi, ""); else strcpy(Form[i][j].gobi_yomi, _Atom(car(cdr(cdr(cell2))))); } else { Form[i][j].gobi_yomi = (U_CHAR *) my_alloc(sizeof(U_CHAR) * strlen(Form[i][j].gobi) + 1); strcpy(Form[i][j].gobi_yomi, Form[i][j].gobi); } j++; cell1 = cdr(cell1); } i++; } }
char *save(char *str) { register char *p; p = (char *)my_alloc((unsigned)((strlen(str) + 1) * sizeof(char))); return strcpy(p, str); }
void menu() { my_initialize_heap(4000); int size; printf("enter a positive interger\n"); scanf_s("%d", &size); int *arr = (int*)my_alloc(sizeof(int) * size); int x; double mean = 0; for (int i = 0; i < size; i++) { scanf_s("%d", &x); arr[i] = x; mean += x; } mean = mean / size; double std_dev = 0; for (int i = 0; i < size; i++) { std_dev += pow(arr[i] - mean, 2); } std_dev = sqrt(std_dev / size); printf("Standard Deviation: %f\n", std_dev); system("pause"); }
/******************************************************************************* get_pair() ******************************************************************************/ pair_ptr_t get_pair(int status) { #ifdef STATISTICS Pair_consumption += sizeof(struct pair); #endif return((pair_ptr_t)my_alloc((my_alloc_size_t)sizeof(struct pair), status)); }
void readcclass(register classrec *cp) { register int n; register defn *dp; defn **dput; char store[MAXDEF]; /* for tags */ cp->weight = getw(InFile); instring(store, MAXDEF); cp->tags = (store[0] == '\0') ? NullTags : save(store); n = getw(InFile); if (n <= 0) badfile(); dput = &(cp->list); /* link on here */ while (n--) { dp = (defn *)my_alloc((unsigned) sizeof(defn)); *dput = dp; dp->cumul = getw(InFile); instring(store, MAXDEF); dp->string = save(store); dput = &(dp->next); } *dput = NULL; /* last one */ }
/******************************************************************************* get_string() Allocate a string of length (how_much - 1) ******************************************************************************/ string_ptr_t get_string(my_alloc_size_t how_much,int status) { #ifdef STATISTICS String_consumption += how_much; #endif return((string_ptr_t)my_alloc(how_much, status)); }
static bool MsgReadErrArray( void ) #endif { int i; char buffer[128]; unsigned msg_shift; msg_shift = _WResLanguage() * MSG_LANG_SPACING; for( i = ERR_FIRST_MESSAGE; i <= ERR_LAST_MESSAGE; i++ ) { #if !defined(__WINDOWS__) if( WResLoadString( &hInstance, i + msg_shift, (lpstr)buffer, sizeof( buffer ) ) <= 0 ) { #else if( LoadString( inst, i + msg_shift, (LPSTR)buffer, sizeof( buffer ) ) <= 0 ) { #endif if( i == ERR_FIRST_MESSAGE ) return( false ); buffer[0] = '\0'; } MsgArray[i - ERR_FIRST_MESSAGE] = my_alloc( strlen( buffer ) + 1 ); if( MsgArray[i - ERR_FIRST_MESSAGE] == NULL ) return( false ); _fstrcpy( MsgArray[i - ERR_FIRST_MESSAGE], buffer ); } return( true ); } #if !defined(__WINDOWS__) bool MsgInit( void ) { char buffer[_MAX_PATH]; #if defined(_PLS) char *fname; char fullpath[_MAX_PATH]; #endif bool rc; hInstance.status = 0; if( _cmdname( buffer ) != NULL ) { rc = OpenResFile( &hInstance, buffer ); #if defined(_PLS) if( !rc ) { _splitpath2( buffer, fullpath, NULL, NULL, &fname, NULL ); _makepath( buffer, NULL, NULL, fname, ".exp" ); _searchenv( buffer, "PATH", fullpath ); if( fullpath[0] != '\0' ) { rc = OpenResFile( &hInstance, fullpath ); } } #endif if( rc ) { MsgReadErrArray(); CloseResFile( &hInstance ); return( true ); } } CloseResFile( &hInstance ); posix_write( STDOUT_FILENO, NO_RES_MESSAGE, NO_RES_SIZE ); return( false ); }
void * my_alloc (int size) { do { while (offset >= 0 && offset < (BLOCKSIZE - size - 8)) { char status = (char) * (data_current + offset); unsigned short bytes = (unsigned char)*(data_current + offset + 1) * 0xFF + (unsigned char)*(data_current + offset + 2); if(status == 0 && bytes >= size) // block is unused and big enough { found_something = 1; used_mem += size; *(data_current + offset) = 1; // used *(data_current + offset + 1) = size / 0xFF; // ... *(data_current + offset + 2) = size % 0xFF; // the next 'size' bytes are used if(bytes != size) { *(data_current + offset + 8 + size) = 0; // used *(data_current + offset + 9 + size) = (bytes - size) / 0xFF; // ... *(data_current + offset +10 + size) = (bytes - size) % 0xFF; // the next 'x' bytes are unsed } void * ret = data_current + (offset + 8)*sizeof(char); offset += 8 + size; return ret; } else // block is used or not big enough { offset += 8 + bytes; printf("bytes: %u\n",bytes); } } if(blocks <= current_block) { data_current = data_start; // go back to the beginning found_something = 0; current_block = 1; } else { data_current = (void*) data_current + sizeof(BLOCKSIZE - 8); current_block++; } } while(found_something == 1 && (used_mem / mem_size) < WASTE_RATE); // no block of the given size found found_something = 0; offset = 0; blocks++; current_block++; data_current = get_block_from_system(); char * end = data_last + BLOCKSIZE - 8; end = data_current; mem_size += BLOCKSIZE; // mark the memory as free *(data_current) = 0; // unused *(data_current+1) = (BLOCKSIZE - 16) / 0xFF; // ... *(data_current+2) = (BLOCKSIZE - 16) % 0xFF; // the next 8176 bytes are unused return my_alloc(size); }
void test_3() { my_initialize_heap(300); printf("-----Test Case 3-----\n"); int *a = (int*)my_alloc(sizeof(int)); printf("int a: %p\t\n", a); int *b = (int*)my_alloc(sizeof(int)); printf("int b: %p\n", b); int *c = (int*)my_alloc(sizeof(int)); printf("int c: %p\n", c); my_free(b); double *d = (double*)my_alloc(sizeof(double)); printf("double d: %p\n", d); int *e = (int*)my_alloc(sizeof(int)); printf("int e: %p\n", e); system("pause"); }
int main() { std::tuple<int, std::string, double> t1; std::cout << "Value-initialized: "; print(t1); std::tuple<int, std::string, double> t2(42, "Test", -3.14); std::cout << "Initialized with values: "; print(t2); std::tuple<char, std::string, int> t3(t2); std::cout << "Implicitly converted: "; print(t3); std::tuple<int, double> t4(std::make_pair(42, 3.14)); std::cout << "Constructed from a pair"; print(t4); // given Allocator my_alloc with a single-argument constructor my_alloc(int) // use my_alloc(1) to allocate 10 ints in a vector std::vector<int, my_alloc> v(10, 1, my_alloc(1)); // use my_alloc(2) to allocate 10 ints in a vector in a tuple std::tuple<int, std::vector<int, my_alloc>, double> t5(std::allocator_arg, my_alloc(2), 42, v, -3.14); }
//! Get memory for new array of length \c sz and fill with zeros KOKKOS_INLINE_FUNCTION static T* get_and_fill(int sz) { if (sz > 0) { T* m = my_alloc(sz); std::memset(m,0,sz*sizeof(T)); return m; } return NULL; }
mb *mb_new(int length) { mb *mb; int size = length + sizeof(mb); mb = my_alloc(size); mb->size = size; return mb; }
/******************************************************************************* get_real() There is no need for a get_integer, as integers fit in a node. ******************************************************************************/ real_ptr_t get_real(int status) { #ifdef STATISTICS Real_consumption += sizeof(real); #endif #ifdef PAIRS_TOGETHER if(status == PERMANENT)status = PERM_STRING; #endif return((real_ptr_t)my_alloc((my_alloc_size_t)sizeof(real), status)); }
/*! * \brief Get memory for new array of length \c sz and fill with * entries from \c src */ KOKKOS_INLINE_FUNCTION static T* strided_get_and_fill(const T* src, int stride, int sz) { if (sz > 0) { T* m = my_alloc(sz); for (int i=0; i<sz; ++i) m[i] = src[i*stride]; return m; } return NULL; }
/******************************************************************************* get_atom(); ******************************************************************************/ atom_ptr_t get_atom(int status) { #ifdef STATISTICS Atom_consumption += sizeof(struct atom); #endif #ifdef PAIRS_TOGETHER if(status == PERMANENT)status = PERM_STRING; #endif return((atom_ptr_t)my_alloc((my_alloc_size_t)sizeof(struct atom), status)); }
/******************************************************************************* get_node() It might be an idea to actually allocate a pair and return the pointer to the head. This would simplify garbage collection. ******************************************************************************/ node_ptr_t get_node(int status) { #ifdef STATISTICS Node_consumption += sizeof(struct node); #endif #ifdef PAIRS_TOGETHER if(status == PERMANENT)status = PERM_STRING; #endif return((node_ptr_t)my_alloc((my_alloc_size_t)sizeof(struct node), status)); }
MEM_ULONG alloc(MEM_ULONG nbytes) { unsigned int sr; sr = spin_lock_irqsave(); MEM_ULONG i = my_alloc(nbytes); if(i != 0) setalign(i,0); spin_unlock_irqrestore(sr); return i; }/*end alloc*/
/*! * \brief Get memory for new array of length \c sz and fill with * entries from \c src */ KOKKOS_INLINE_FUNCTION static T* get_and_fill(const T* src, int sz) { if (sz > 0) { T* m = my_alloc(sz); T* p = m; for (int i=0; i<sz; ++i) new (p++) T(*(src++)); return m; } return NULL; }
static void *jm_malloc_char(int size) { if (buffer_idx + size >= JUMAN_MALLOC_SIZE) { if (buffer_ptr_num == 99) error(1, "Can't allocate memory"); buffer_ptr[buffer_ptr_num++] = my_alloc(JUMAN_MALLOC_SIZE); buffer_idx = 0; } buffer_idx += size; return buffer_ptr[buffer_ptr_num - 1] + buffer_idx - size; }
void *my_memdup(const void *mem, size_t bype_size) { void *new_mem; if (mem) { new_mem = my_alloc(bype_size); memcpy(new_mem, mem, bype_size); } else new_mem = NULL; return new_mem; }
void load_user_dicts_kzl(char * list_name, char * point) /*-----------------17-02-93 03:30pm----------------- Function loads user's dictionaries into memory by list of vocs. --------------------------------------------------*/ { char w[MAXPATH], nm[MAXPATH]; FILE * lst; int16_t type; int16_t errorNo = 0; unload_user_dicts(); lst = fopen(list_name, "rt"); if (lst < 0) { return; } while (fgets(w, MAXPATH, lst) != NULL) { if (real_voc_no == MAX_VOC_NUMBER) { errorNo = VOC_TOOLARGELIST; break; } if (!parce_voc_list_record(w, nm, &type)) { continue; } if ((point = my_alloc(VOCMEMSIZE)) == NULL) { errorNo = VOC_NOTLOADED; break; } if (LoadUserDict(nm, point, VOCMEMSIZE, &(voc_array[real_voc_no].voc)) == 0L) { my_free(point/*, 0*/); errorNo = VOC_NOTLOADED; break; } real_voc_no++; } fclose(lst); if (errorNo != 0) { ErrorExit( /*ERR_voc,*/errorNo); } return; }
MEM_ULONG Drv_calloc(MEM_ULONG size,MEM_ULONG n) { MEM_ULONG rr; unsigned int sr; sr = spin_lock_irqsave(); rr = my_alloc(size * n); if(rr != 0) { setalign(rr,0); memset((void *)rr,0,size * n); } spin_unlock_irqrestore(sr); return rr; }
MEM_ULONG alignAlloc(MEM_ULONG align,MEM_ULONG size) { unsigned int sr; sr = spin_lock_irqsave(); MEM_ULONG i2 = 0; MEM_ULONG i = my_alloc(size + align); if(i != 0) { i2 = (i + align - 1) & ~(align - 1); setalign(i2, i2 - i); } spin_unlock_irqrestore(sr); return i2; }/*end alloc*/
char *my_strndup(const char *str, size_t n) { char *new_str; if (str) { new_str = (char *)my_alloc(n + 1); strncpy(new_str, str, n); new_str[n] = '\0'; } else new_str = NULL; return new_str; }
char *my_strdup(const char *str) { char *new_str; size_t length; if (str) { length = STRLEN1(str); new_str = (char *)my_alloc(length); memcpy(new_str, str, length); } else new_str = NULL; return new_str; }