🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by (fir) Mon, 11 Sep 2017 17:54
resizable chunks and typesafety
#309444
Author: (fir)
Date: Mon, 11 Sep 2017 17:54
63 lines
4319 bytes
(oryginalnie napisane po angielsku i nie tlumacze bo nie ma sensu) (wogole dziwny efekt ze ostatnimi czasy o wiele latwiej mi sie pisze po angielsku niz po polsku - po angielsku mozna pisac czesto jakby bardziej sloganowo co po polsku jakos wydaje mi sie nie uchodzi)

it seems to me (though i got it not yet much groundfully rethinked) that there is some kind of typesafety that maybe would be much important that 'const' typesafety 

this typesafety is about if given pointer pr chunk is resizable (reallocable) or not (if it is from bss or root on heep in other words) 

if some use only pointers it maybe may be not much seen as passing a pointers for reallocation is probably considered as bad practice (which is realated to amount of  mistakes and deadly arrors that it can bring)  but if some would use chunks as I do 

(chunk is pair of pointers describing some area of ram  typedef struct{ char* beg,* end;} chunk; ) 

then is quite natural to use two kinds of them, one is 'static chunk' (which describes 
some area of static ram, or some sub areo on heap) or 'relocable chunk' which is just produced by allock on heap and hass additional ability - can be reallocked/resized 

this relocable chunk can be created for example like this 

chunk AllocChunk(chunk x, int size) 
{ 
    x.beg = (char*) realloc(x.beg, size ); 
    x.end = x.beg + size -1; 
    return x; 
} 


(this function both allocks and reallocks but it imo is safer to use than one that only mallocks) 

the problem is that c if im not wrong not gives an expression to express this 'typesafety' i mean i would probably like to define two types 

typedef struct 
 { 
   char* beg,* end; 
 } 
   chunk; 

and 

typedef struct 
 { 
   char* beg,* end; 
 } 
   relocable_chunk; //well maybe shorter name 

and use those types in apiwhere it is appropriate 
- some api should only take "resizable_chunk" as it will realloc it internally 
- some other api will just expact  "chunks" (as it will just not resize it) (ofc rezizable_chunks can be also passed here 

(say "most" api will use chunks, though im not sure but probably as those on chunks is probaby faster and maybe should be used more, this based on reallocable_chunks will be maybe slower (though as i write in other post as reallock dont do such quite much memcopying as one could think, it can alos be usually relatively fast, depends on usage maybe) 

in present c, when you pass chunk to function that expect reasizable_chunk it will be compile error and thiat is ok (some newb van cast it by force but as it will crash it will be learned anyway ;c) 

more problem is in different way pass realocable_chunk ehen function expect chunk 
- idally that should work seamles but now it 
needs to work  "  *(chunk*)& " type of asting 

my other idea was to define resizable chunk as union 

union resizable_chunk 
{ 
   chunk c; 
}; 

and now it needs writing "my_data.c" instead of "*(chunk*)& my_data" shorter but im not sure if so much better 

question: is there yet some way to improve that? 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads