Re: void types and how to use them efficiently?




"cognacc" <michael.cognacc@xxxxxxxxx> wrote in message
news:1c0950c9-ee99-4efe-8b24-8717ed6eacd4@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi
I have implemented a program with many different types.
Where i use void as a generic object (pointer).

I have a type i call Record

struct Record {
unsigned int RecordID;
void *fieldtype; // don't know type of field beforehand
struct Record *next_record; // i actually use TAILQ macro
};


<snip>

well, there are many ways to dynamically manage types, and each has good and
bad points, so the ideal choice ultimately depends on ones' requirements...


for my uses, I have a custom memory allocator (actually, it is a full
garbage collector as well, but this is besides the point), where an
allocation request will typically provide 2 pieces of information:
a type name (generally semantic);
the object size.

internally, a large hash table is used, and this essentially converts the
type name into a hash index (may be used directly for more
performance-critical code, but in generally it is not used that much).

this is also used for accessing info structs (per object, the info struct
can be fetched directly, since internally the type is an integer).

however, externally, it all just looks like an ordinary void pointer to an
ordinary chuck of memory, nevermind that one can later fetch the type name,
do various per-type operations, ...

this is very convinient to use from C, but it is not the highest performance
option.


another approach is what is known as tagged references, where the reference
is typically an integer-based type (32 or 64 bit integers), and where the
type is usually embedded directly into the reference (with other bits for
either holding a literal value, or referring to an in-heap object).

some people directly encode pointers into these references when referring to
objects, but personally I have found it more effective (when I use this
strategy) to instead encode an "object index" into the reference, which when
needed can then be resolved into the actual memory address of the object.

possible reasons for doing so:
simplifies things like precise GC and compacting collectors;
may only need 32 bit references on 64-bit computers (where otherwise one
would need 64 bits for a pointer-based reference);
....

though, it may involve a slight overhead when accessing an object (the need
to resolve the index to a pointer), it usually improves performance for many
other operations.


similarly, tagged references tend to be generally faster than "magic void
pointers"...

however, there is a downside:
they tend to be much less convinient to work with in C, and it may also be a
lot harder to have independent extension of the typesystem (since types
depend a lot on the tagging scheme, ... much more "centralization" is needed
to make all this work, ...).

or such...



.



Relevant Pages

  • [PATCH 12/15] dm: add exports
    ... Export core device-mapper functions for manipulating mapped devices ... void dm_get ... struct dm_target; ... * Drop the reference with dm_put when you finish with the object. ...
    (Linux-Kernel)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Re: A C Adventure: your comments are welcome
    ... void * usrRightSegment; ... That is a string is a binary tree of segments, ... struct inside a struct either as the thing itself or as a pointer ... because C makes it easy to change pointer type. ...
    (comp.lang.c)
  • Re: Strong thread safety and lock free?
    ... you definitely can pack pointer and counter into single word. ... No, there is no emulation of locks, just 2 reference counters ... Automatic alignment but typically no explicit alignment required ... static void debuglog(const char* fmt, ...
    (comp.programming.threads)
  • Re: Some issue with pointers
    ... typedef struct { ... void *ptr; ... and a pointer to the items structure. ...
    (comp.lang.c)