void st_memory(void) { enum { P = N/2/sizeof(void*) }; static OBJ arr[P]; useclass(Counter, AutoRelease); OBJ ar = gnew(AutoRelease); size_t sz = gsize(Counter); size_t i; int lvl; // allocator warm up for (i = 0; i < P; i++) arr[i++] = malloc(sz); for (i = 0; i < P; i++) free(arr[i++]); i = 0; STEST( "malloc", P, arr[i++] = malloc(sz) ); i = 0; STEST( "free", P, free(arr[i++]) ); i = 0; STEST( "alloc + init", P, arr[i++] = ginit(galloc(Counter)) ); i = 0; STEST( "retain", P, gretain(arr[i++]) ); i = 0; lvl = cos_logmsg_setLevel(COS_LOGMSG_WARN); STEST( "autoRelease", P, gautoRelease(arr[i++]) ); cos_logmsg_setLevel(lvl); i = 0; STEST( "release", P, grelease(arr[i++]) ); STEST( "alloc + init + release", P, grelease(ginit(galloc(Counter))) ); grelease(ar); }
#include <cos/Object.h> #include <cos/Proxy.h> #include <cos/gen/message.h> #include <cos/gen/object.h> #include <stdlib.h> // ----- makclass(Proxy); // ----- constructor, destructor defmethod(OBJ, ginitWith, Proxy, Object) self->obj = gretain(_2); retmethod(_1); endmethod defmethod(OBJ, gdeinit, Proxy) if (self->obj) grelease(self->obj), self->obj = 0; retmethod(_1); endmethod // ----- understanding (1+3+7+15+31=57 cases) // ----- rank 1 (2^1-1=1 case) defmethod(OBJ, gunderstandMessage1, Proxy, (SEL)sel)
defmethod(OBJ, ginitWith2 , TP, Functor, Int) // generator retmethod(ginitWith3(galloc(TL),_2,aTRef(0,0),_3)); endmethod defalias (OBJ, (ginitWith3)gnewWith3, TP, Functor, T, Int); defmethod(OBJ, ginitWith3 , TP, Functor, T, Int) // generator retmethod(ginitWith3(galloc(TL),_2,_3,_4)); endmethod defmethod(OBJ, ginitWith3, TL, Functor, T, Int) // generator defnext(OBJ, ginitWith , TL, Int); // forward to dynamic array next_method(self, self4); PRT(_1); self->generator = gretain(_2); if (self3->size > 0) gappend(_1,_3); UNPRT(_1); retmethod(_1); endmethod // ----- destructor defmethod(OBJ, gdeinit, TL) if (self->generator) // take care of protection cases grelease(self->generator); next_method(self); retmethod(_1);
{ useclass(ExBadAlloc); char *cpy = malloc(strlen(str)+1); if (!cpy) THROW(ExBadAlloc); return strcpy(cpy, str); } defmethod(OBJ, ginit, Exception) self->obj = 0; self->str = 0; retmethod(_1); endmethod defmethod(OBJ, ginitWithObj, Exception, (OBJ)obj) self->obj = gretain(obj); self->str = 0; retmethod(_1); endmethod defmethod(OBJ, ginitWithStr, Exception, (STR)str) self->obj = 0; self->str = str_dup(str); retmethod(_1); endmethod defmethod(OBJ, ginitWithObjStr, Exception, (OBJ)obj, (STR)str) self->obj = gretain(obj); self->str = str_dup(str); retmethod(_1); endmethod