void mbus_data_free( MObject * mdata ) { M_OBJECT_ASSERT( mdata, MDATA ); g_byte_array_free( M_DATA( mdata )->array, TRUE ); M_OBJECT_FREE( mdata ); }
/* * The actual free() implementation. */ void free(void *x) { struct mheader *mh, *mhnext, *mhprev; if (x==NULL) { /* safest practice */ return; } /* Consistency check. */ if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) { warnx("free: Internal error - local data corrupt"); errx(1, "free: heapbase 0x%lx; heaptop 0x%lx", (unsigned long) __heapbase, (unsigned long) __heaptop); } /* Don't allow freeing pointers that aren't on the heap. */ if ((uintptr_t)x < __heapbase || (uintptr_t)x >= __heaptop) { errx(1, "free: Invalid pointer %p freed (out of range)", x); } #ifdef MALLOCDEBUG warnx("free: about to free %p", x); __malloc_dump(); #endif mh = ((struct mheader *)x)-1; if (!M_OK(mh)) { errx(1, "free: Invalid pointer %p freed (corrupt header)", x); } if (!mh->mh_inuse) { errx(1, "free: Invalid pointer %p freed (already free)", x); } /* mark it free */ mh->mh_inuse = 0; /* wipe it */ __malloc_deadbeef(M_DATA(mh), M_SIZE(mh)); /* Try merging with the block above (but not if we're at the top) */ mhnext = M_NEXT(mh); if (mhnext != (struct mheader *)__heaptop) { __malloc_trymerge(mh, mhnext); } /* Try merging with the block below (but not if we're at the bottom) */ if (mh != (struct mheader *)__heapbase) { mhprev = M_PREV(mh); __malloc_trymerge(mhprev, mh); } #ifdef MALLOCDEBUG warnx("free: freed %p", x); __malloc_dump(); #endif }
void mbus_data_set( MObject * mdata, const guint8 * str, guint len ) { GByteArray * array; M_OBJECT_ASSERT( mdata, MDATA ); array = M_DATA( mdata )->array; g_byte_array_remove_range( array, 0, array->len ); g_byte_array_append( array, str, len ); }
void mbus_data_as_string( MObject * mdata, GString * buf ) { GByteArray * tmp = g_byte_array_new(); M_OBJECT_ASSERT( mdata, MDATA ); base64encode( M_DATA( mdata )->array, tmp ); g_byte_array_prepend( tmp, ( guchar * ) "<", 1 ); g_byte_array_append( tmp, ( guchar * ) ">", 1 ); g_string_append_len( buf, ( gchar * ) tmp->data, tmp->len ); g_byte_array_free( tmp, TRUE ); }
MObject * mbus_data_copy( const MObject * mdata ) { return mbus_data_new( M_DATA( mdata )->array->data, M_DATA( mdata )->array->len, FALSE ); }
/* * malloc itself. */ void * malloc(size_t size) { struct mheader *mh; uintptr_t i; size_t rightprevblock; if (__heapbase==0) { __malloc_init(); } if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) { warnx("malloc: Internal error - local data corrupt"); errx(1, "malloc: heapbase 0x%lx; heaptop 0x%lx", (unsigned long) __heapbase, (unsigned long) __heaptop); } #ifdef MALLOCDEBUG warnx("malloc: about to allocate %lu (0x%lx) bytes", (unsigned long) size, (unsigned long) size); __malloc_dump(); #endif /* Round size up to an integral number of blocks. */ size = ((size + MBLOCKSIZE - 1) & ~(size_t)(MBLOCKSIZE-1)); /* * First-fit search algorithm for available blocks. * Check to make sure the next/previous sizes all agree. */ rightprevblock = 0; for (i=__heapbase; i<__heaptop; i += M_NEXTOFF(mh)) { mh = (struct mheader *) i; if (!M_OK(mh)) { errx(1, "malloc: Heap corrupt; header at 0x%lx" " has bad magic bits", (unsigned long) i); } if (mh->mh_prevblock != rightprevblock) { errx(1, "malloc: Heap corrupt; header at 0x%lx" " has bad previous-block size %lu " "(should be %lu)", (unsigned long) i, (unsigned long) mh->mh_prevblock << MBLOCKSHIFT, (unsigned long) rightprevblock << MBLOCKSHIFT); } rightprevblock = mh->mh_nextblock; /* Can't allocate a block that's in use. */ if (mh->mh_inuse) { continue; } /* Can't allocate a block that isn't big enough. */ if (M_SIZE(mh) < size) { continue; } /* Try splitting block. */ __malloc_split(mh, size); /* * Now, allocate. */ mh->mh_inuse = 1; #ifdef MALLOCDEBUG warnx("malloc: allocating at %p", M_DATA(mh)); __malloc_dump(); #endif return M_DATA(mh); } if (i!=__heaptop) { errx(1, "malloc: Heap corrupt; ran off end"); } /* * Didn't find anything. Expand the heap. */ mh = __malloc_sbrk(size + MBLOCKSIZE); if (mh == NULL) { return NULL; } mh->mh_prevblock = rightprevblock; mh->mh_magic1 = MMAGIC; mh->mh_magic2 = MMAGIC; mh->mh_pad = 0; mh->mh_inuse = 1; mh->mh_nextblock = M_MKFIELD(size + MBLOCKSIZE); #ifdef MALLOCDEBUG warnx("malloc: allocating at %p", M_DATA(mh)); __malloc_dump(); #endif return M_DATA(mh); }
T& Matrix<T>::right(const unsigned int row, const unsigned int col) { return M_DATA(row,(col < M_Y_DIM + 1 ? col + 1 : 0)); }
T& Matrix<T>::left(const unsigned int row, const unsigned int col) { return M_DATA(row,(col == 0 ? M_Y_DIM - 1 : col - 1)); }
T& Matrix<T>::down(const unsigned int row, const unsigned int col) { return M_DATA(row, col); }