Thread: What recommendations do peops have for object management?

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,429

    What recommendations do peops have for object management?

    I've recently realised the best way to share objects into lua and anything the lua code passes those objects onto without the risk of mixing up pointers from different allocation systems (via mistakes or tom-foolery on user side) is to just use a list of IDs/Indexes in the source library and pass those instead, this is what I've come up with so far for abstracting out details that the object management section of the library doesn't need to know while providing a way other sections of the library can determine if they've been given an invalid id or tell the object manager some custom method needs to be called prior to releaseing the memory of the object being managed:
    Code:
    ...
    struct paw_block;
    
    typedef void (*paw__forget)( struct PAW *P, struct paw_block *block );
    
    struct paw_block
    {
    	size_t size;
    	char const *desc;
    	void *addr;
    	/* This should ONLY clean the contents of whatever addr points to, the
    	 * memory & index will be realeased by paw__del_block() */
    	paw__forget forget;
    };
    
    struct paw_blocks
    {
    	size_t size;
    	int have;
    	int used;
    	void *ud;
    	struct paw_block *array;
    };
    ...
    #include "_paw.h"
    PAW_HIDDEN void paw__del_block( struct PAW *P, int id )
    {
    	if ( id >= 0 ) 
    	{
    		struct paw_block *block = P->blocks->array + id;
    		
    		if ( block->forget )
    			block->forget( P, block );
    		
    		P->alloc( P->ud, block->addr, block->size, 0 );
    		memset( block, 0, sizeof(struct paw_block) );
    	}
    }
    
    PAW_HIDDEN struct paw_block* paw__get_block( struct PAW *P, int id )
    {
    	if ( id >= 0 )
    	{
    		struct paw_blocks *blocks = P->blocks;
    		
    		if ( blocks && id < blocks->have )
    		{
    			return blocks->array + id;
    		}
    	}
    	
    	return NULL;
    }
    
    PAW_HIDDEN int paw__new_block
    (
    	struct PAW *P
    	, char const *desc
    	, size_t want
    	, struct paw_block **BLOCK
    )
    {
    	int i = 0;
    	struct paw_block *block = NULL;
    	struct paw_blocks *blocks = P->blocks;
    	
    	if ( blocks )
    	{
    		for ( ; !block && i < blocks->have; ++i )
    		{
    			block = blocks->array + i;
    			
    			if ( !(block->addr) )
    				break;
    				
    			block = NULL;
    		}
    	}
    	
    	if ( !block )
    	{
    		size_t need = blocks ? blocks->size + BUFSIZ : BUFSIZ;
    		void* temp = P->alloc
    		(
    			P->ud, blocks, blocks ? blocks->size : 0, need
    		);
    		
    		if ( !temp )
    			return -1;
    		
    		P->blocks = blocks = temp;
    		blocks->array = (struct paw_block*)(blocks + 1);
    		blocks->size = need;
    		blocks->have =
    			(need - sizeof(struct paw_blocks)) / sizeof(struct paw_block);
    		block = blocks->array + i;
    	}
    	
    	block->addr = P->alloc( P->ud, NULL, 0, want );
    	
    	if ( block->addr )
    	{
    		block->size = want;
    		/* Caller is responsible for giving this a non-null value */
    		block->forget = NULL;
    		*BLOCK = block;
    		return i;
    	}
    	
    	*BLOCK = NULL;
    	return -1;
    }
    Does anyone have any recommendations or faults to point out?

  2. #2

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,429
    Encountering a segfault that I can't track down the source of, I've uploaded the current state of code to the gitlab repository (I switched from github to gitlab after having enough of copying my SSH token from file, passwords are more convenient & secure if you can keep them in your head which I do). Just click the paw link in my signature, so far I can only determine where the segfault occurs, not what causes it, it occurs in paw_get_ptr() when it tries to compare the desc parameter to the object description which should've been set under the test scenario, sure later I will need to test for NULL but right now that would actually interfere with testing. The files to look at are chkpaw.c (where the testing is done), app.c (which has not directly complained of issues), block.c (which I haven't got round to renaming to ptr.c, it holds the object management part of paw), and txt.c (which holds the initialisation code for the pointed objects), I'll later switch that strcmp() in paw_get_ptr() to a strstr() call that checks if it can find the desired description at the start of the object's description which would indicate the object is directly inherited via a member placed at the top making it safe to cast to the inherit object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question: Determining object scope from root object
    By Imanuel in forum C++ Programming
    Replies: 6
    Last Post: 12-25-2012, 11:20 AM
  2. Trouble storing ifstream object in pair object
    By Programmer_P in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2012, 01:20 AM
  3. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  4. Dynamic Object Creation (and Management)...
    By Comrade_Yeti in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2005, 01:44 PM

Tags for this Thread