A contribution and apology

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 03/01/98


I didn't realize John had found his own error before I sent my
message, so now I should apologize for wasting everyone's time
(including my own) on a pointless message.

To make up for that oversight, I have included two little macros in
the same vein as my older REMOVE_FROM_LIST replacement.  The two below
and REMOVE_FROM_LIST all use a gcc typedef extension, so they only
work on gcc (to my knowledge, no other compiler supports the same
typedef extension).

These are replacements for CREATE and RECREATE that automatically
detect the type of the variable passed to it, thus making the need for
the second argument (the type of the variable) obsolete, e.g.,

  /* old way */
  int *int_array;
  CREATE(int_array, int, 5);

  /* new way */
  int *int_array;
  Allocate(int_array, 5);

The macros have one little bit of "hackish" code, the being gcc's
"typedef" line to get the type of the passed variable, which is
normally used as, "typedef mytype = (var);" which creates a usable
type 'mytype'.  The problem being that if 'var' is a pointer (e.g.,
int *), then 'mytype' is equivalent to "int *".  This wouldn't be bad
if calloc()/malloc() didn't want to know the size of the variable.
The size of a pointer is always 4-bytes (32 bits), so that doesn't
help us unless we're allocating memory to an integer pointer.  So, I
made a hack, addressing the object as an array (even though it isn't
even been allocated memory, yet), and thus, the type is equal to
obj[0] not obj...that's probably unclear (I can't think of how to say
it, even though I know what I'm saying :P).  Anyway, if you don't
understand, that's okay.  I did some (minimal) testing, and it
works.  Here are the macros,

  #define Allocate(obj, size) \
    do {                                                \
      typedef _tobj = (obj)[0];                         \
      (obj) = (_tobj *) calloc(sizeof(_tobj), (size));  \
      if (!obj) {                                       \
        perror("calloc");                               \
        abort();                                        \
      }                                                 \
    while (0)

  #define Reallocate(obj, size) \
    do {                                                \
      typedef _tobj = (obj)[0];                         \
      (obj) = (_tobj *) realloc((obj), sizeof(_tobj)*(size));   \
      if (!obj) {                                       \
        perror("realloc");                              \
        abort();                                        \
      }                                                 \
    while (0)

Interestingly enough, it's possible to use realloc() to simulate
malloc() and free(), too, so it'd be possible to have one macro that
does all three.  BTW, the above isn't cut-and-pasted from working
code, so it should be considered Mailer Code(tm).

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST