Re: void types and how to use them efficiently?
- From: "BGB / cr88192" <cr88192@xxxxxxxxxxx>
- Date: Sat, 19 Sep 2009 16:50:47 -0700
"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...
.
- References:
- void types and how to use them efficiently?
- From: cognacc
- void types and how to use them efficiently?
- Prev by Date: A syntax oditty 3000
- Next by Date: Re: Plz explain me the following code
- Previous by thread: Re: void types and how to use them efficiently?
- Next by thread: Opening a file of user's choice
- Index(es):
Relevant Pages
|