Open Dynamics Engine

obstack.h

00001 /*************************************************************************
00002  *                                                                       *
00003  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
00004  * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
00005  *                                                                       *
00006  * This library is free software; you can redistribute it and/or         *
00007  * modify it under the terms of EITHER:                                  *
00008  *   (1) The GNU Lesser General Public License as published by the Free  *
00009  *       Software Foundation; either version 2.1 of the License, or (at  *
00010  *       your option) any later version. The text of the GNU Lesser      *
00011  *       General Public License is included with this library in the     *
00012  *       file LICENSE.TXT.                                               *
00013  *   (2) The BSD-style license that is included with this library in     *
00014  *       the file LICENSE-BSD.TXT.                                       *
00015  *                                                                       *
00016  * This library is distributed in the hope that it will be useful,       *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00019  * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
00020  *                                                                       *
00021  *************************************************************************/
00022 
00023 #ifndef _ODE_OBSTACK_H_
00024 #define _ODE_OBSTACK_H_
00025 
00026 #include "objects.h" 
00027 
00028 // each obstack Arena pointer points to a block of this many bytes
00029 #define dOBSTACK_ARENA_SIZE 16384
00030 
00031 
00032 struct dObStack : public dBase {
00033   struct Arena {
00034     Arena *next;  // next arena in linked list
00035     size_t used;     // total number of bytes used in this arena, counting
00036   };        //   this header
00037 
00038   Arena *first;      // head of the arena linked list. 0 if no arenas yet
00039   Arena *last;    // arena where blocks are currently being allocated
00040 
00041   // used for iterator
00042   Arena *current_arena;
00043   size_t current_ofs;
00044 
00045   dObStack();
00046   ~dObStack();
00047 
00048   void *alloc (size_t num_bytes);
00049   // allocate a block in the last arena, allocating a new arena if necessary.
00050   // it is a runtime error if num_bytes is larger than the arena size.
00051 
00052   void freeAll();
00053   // free all blocks in all arenas. this does not deallocate the arenas
00054   // themselves, so future alloc()s will reuse them.
00055 
00056   void *rewind();
00057   // rewind the obstack iterator, and return the address of the first
00058   // allocated block. return 0 if there are no allocated blocks.
00059 
00060   void *next (size_t num_bytes);
00061   // return the address of the next allocated block. 'num_bytes' is the size
00062   // of the previous block. this returns null if there are no more arenas.
00063   // the sequence of 'num_bytes' parameters passed to next() during a
00064   // traversal of the list must exactly match the parameters passed to alloc().
00065 };
00066 
00067 
00068 #endif