#include <stdlib.h> /* malloc(), free(), abort() */
#include <string> #include <sstream> #if defined TEST | defined DEBUG #include <iostream> #endif using namespace std; #include "mallok.h" void *mallok::alloc_1d(int x, int size) { void *h = malloc(x*size); #if defined DEBUG cerr << "mallok::allok_1d: x=" << x << " size=" << size << ’\n’; cerr << "mallok::allok_1d: h=" << h << ’\n’; #endif #if defined TEST if(!h) { cerr << "mallok::allok_1d: out of memory (" << x << "," << size << ")"; abort(); } #endif return h; } void mallok::free_1d(void *h) { #if defined DEBUG cerr << "mallok::free_1d(" << h << ")\n"; #endif free(h); } void **mallok::alloc_2d(int y, int x, int size) { void **h = (void **)mallok::alloc_1d(y, sizeof(void *)); h[0] = (void *)mallok::alloc_1d(y*x,size); int i; for(i=1; i<y; i++) { h[i] = (unsigned char *)h[i-1] + x*size; } return h; } void mallok::free_2d(void **h) { free_1d(h[0]); free_1d((void *)h); } void ***mallok::alloc_3d(int z, int y, int x, int size) { void ***h = (void ***)mallok::alloc_2d(z,y,sizeof(void *)); h[0][0] = (void *)mallok::alloc_1d(z*y*x,size); int i; for(i=1; i<z*y; i++) { h[i/y][i%y] = (unsigned char *)h[0][i-1] + x*size; } return h; } void mallok::free_3d(void ***h) { free_1d(h[0][0]); free_2d((void **)h); } //#define _MAIN_ #ifdef _MAIN_ int main() { mallok m; m.alloc_1d(1,1); } #endif |