Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] Re: very simple specvecs
Home

[Freeciv-Dev] Re: very simple specvecs

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Jason Dorje Short <jdorje@xxxxxxxxxxxx>
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: very simple specvecs
From: Vasco Alexandre Da Silva Costa <vasc@xxxxxxxxxxxxxx>
Date: Sat, 15 May 2004 18:08:23 +0100 (WET DST)

On Sat, 15 May 2004, Vasco Alexandre Da Silva Costa wrote:

> > Vasco Alexandre Da Silva Costa wrote:
> >
> > > Just malloc a chunk and store the size before the beggining of the array.
> > > Then return a pointer to the beggining of the array. To know the size,
> > > access the memory before the pointer to the beggining. That should do what
> > > you want.

typedef foo * foo_vector;

void foo_vector_init(foo_vector *tthing)
{
  *tthing = NULL;
}

int foo_vector_size(foo_vector *tthing)
{
  return ((*tthing) ? ((int *)(*tthing)) - 1 : 0);
}

int foo_vector_reserve(foo_vector *tthing, int size)
{
  int *ptr;

  ptr = *tthing;

  ptr = realloc(ptr, sizeof(int) + sizeof(foo) * size);
  *ptr = size;

  ptr++;

  *tthing = (foo_vector)ptr + 1;
}

This version has one advantange. It retains the hability that specvecs
already have that even if you forget to call init on a static variable, it
is still init. Don't know if this is significant or not, it does add one
branch on the size check which should be cheaper.

---
Vasco Alexandre da Silva Costa @ Instituto Superior Tecnico, Lisboa



[Prev in Thread] Current Thread [Next in Thread]