Commit fb452643 authored by osku's avatar osku

Add ib_vector datatype.

parent 3d11e335
......@@ -76,7 +76,7 @@ EXTRA_DIST = include/btr0btr.h include/btr0btr.ic include/btr0cur.h include/btr
include/univ.i include/usr0sess.h include/usr0sess.ic include/usr0types.h \
include/ut0byte.h include/ut0byte.ic include/ut0dbg.h include/ut0lst.h \
include/ut0mem.h include/ut0mem.ic include/ut0rnd.h include/ut0rnd.ic \
include/ut0sort.h include/ut0ut.h include/ut0ut.ic
include/ut0sort.h include/ut0ut.h include/ut0ut.ic include/ut0vec.h include/ut0vec.ic
# Don't update the files from bitkeeper
%::SCCS/s.%
#ifndef IB_VECTOR_H
#define IB_VECTOR_H
#include "univ.i"
#include "mem0mem.h"
typedef struct ib_vector_struct ib_vector;
/* An automatically resizing vector datatype with the following properties:
-Contains void* items.
-The items are owned by the caller.
-All memory allocation is done through a heap owned by the caller, who is
responsible for freeing it when done with the vector.
-When the vector is resized, the old memory area is left allocated since it
uses the same heap as the new memory area, so this is best used for
relatively small or short-lived uses.
*/
/********************************************************************
Create a new vector with the given initial size. */
ib_vector*
ib_vector_create(
/*=============*/
/* out: vector */
mem_heap_t* heap, /* in: heap */
ulint size); /* in: initial size */
/********************************************************************
Push a new element to the vector, increasing its size if necessary. */
void
ib_vector_push(
/*===========*/
ib_vector* vec, /* in: vector */
void* elem); /* in: data element */
/********************************************************************
Get the number of elements in the vector. */
UNIV_INLINE
ulint
ib_vector_size(
/*===========*/
/* out: number of elements in vector */
ib_vector* vec); /* in: vector */
/********************************************************************
Get the n'th element. */
UNIV_INLINE
void*
ib_vector_get(
/*==========*/
/* out: n'th element */
ib_vector* vec, /* in: vector */
ulint n); /* in: element index to get */
/* See comment at beginning of file. */
struct ib_vector_struct {
mem_heap_t* heap; /* heap */
void** data; /* data elements */
ulint used; /* number of elements currently used */
ulint total; /* number of elements allocated */
};
#ifndef UNIV_NONINL
#include "ut0vec.ic"
#endif
#endif
/********************************************************************
Get number of elements in vector. */
UNIV_INLINE
ulint
ib_vector_size(
/*===========*/
/* out: number of elements in vector */
ib_vector* vec) /* in: vector */
{
return(vec->used);
}
/********************************************************************
Get n'th element. */
UNIV_INLINE
void*
ib_vector_get(
/*==========*/
/* out: n'th element */
ib_vector* vec, /* in: vector */
ulint n) /* in: element index to get */
{
ut_a(n < vec->used);
return(vec->data[n]);
}
......@@ -19,6 +19,6 @@ include ../include/Makefile.i
noinst_LIBRARIES = libut.a
libut_a_SOURCES = ut0byte.c ut0dbg.c ut0mem.c ut0rnd.c ut0ut.c
libut_a_SOURCES = ut0byte.c ut0dbg.c ut0mem.c ut0rnd.c ut0ut.c ut0vec.c
EXTRA_PROGRAMS =
#include "ut0vec.h"
#ifdef UNIV_NONINL
#include "ut0vec.ic"
#endif
#include <string.h>
/********************************************************************
Create a new vector with the given initial size. */
ib_vector*
ib_vector_create(
/*=============*/
/* out: vector */
mem_heap_t* heap, /* in: heap */
ulint size) /* in: initial size */
{
ib_vector* vec;
ut_a(size > 0);
vec = mem_heap_alloc(heap, sizeof(*vec));
vec->heap = heap;
vec->data = mem_heap_alloc(heap, sizeof(void*) * size);
vec->used = 0;
vec->total = size;
return(vec);
}
/********************************************************************
Push a new element to the vector, increasing its size if necessary. */
void
ib_vector_push(
/*===========*/
ib_vector* vec, /* in: vector */
void* elem) /* in: data element */
{
if (vec->used >= vec->total) {
void** new_data;
ulint new_total = vec->total * 2;
new_data = mem_heap_alloc(vec->heap,
sizeof(void*) * new_total);
memcpy(new_data, vec->data, sizeof(void*) * vec->total);
vec->data = new_data;
vec->total = new_total;
}
vec->data[vec->used] = elem;
vec->used++;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment