Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

array.h

Go to the documentation of this file.
00001 // See the end of this file for license information.
00002 
00003 #ifndef TORSION_ARRAY_H
00004 #define TORSION_ARRAY_H
00005 
00006 #include "virtmem.h"
00007 #include "physmem.h"
00008 #include "types.h"
00009 
00013 
00014 template <class T>
00015 class Array {
00016 protected:
00017   T* data;        
00018   Size used_size;     
00019 
00020   Size capacity;
00021   Size init_capacity;     
00022   bool physical;      
00023   static const Size INITIAL_VIRTUAL_CAPACITY = 8;
00024   static const Size INITIAL_PHYSICAL_CAPACITY = PAGE_SIZE / sizeof(T);
00025 
00026 public:
00028   Array(bool physical = false, Size init_capacity = 0) {
00029     init(physical, init_capacity);
00030   }
00031 
00037   void
00038   init(bool physical = false, Size init_capacity = 0) {
00039     used_size = 0;
00040     capacity = 0;
00041     data = NULL;
00042     this->physical = physical;
00043     this->init_capacity = init_capacity;
00044   }
00045 
00048   inline Size
00049   size() {
00050     return used_size;
00051   }
00052 
00055   inline void
00056   set_size(unsigned new_size) {
00057     used_size = new_size;
00058   }
00059 
00061   inline Size
00062   get_capacity() {
00063     return capacity;
00064   }
00065 
00068   void
00069   grow(bool set_size_too = false);
00070 
00072   inline void
00073   ensure_capacity() {
00074     if (used_size >= capacity)
00075       grow();
00076   }
00077 
00080   inline T&
00081   append(T& element) {
00082     used_size++;
00083     ensure_capacity();
00084     data[used_size - 1] = element;
00085     return data[used_size - 1];
00086   }
00087 
00089   inline T&
00090   operator [](unsigned index) {
00091     return data[index];
00092   }
00093 
00095   inline void
00096   clear() {
00097     if (data) {
00098       if (physical)
00099         physical_pages.free_page(data);
00100       else
00101         virtual_mem->free(data);
00102     }
00103     data = NULL;
00104     used_size = 0;
00105     capacity = 0;
00106   }
00107 };
00108 
00109 template <class T>
00110 void
00111 Array<T>::grow(bool set_size_too) {
00112   if (physical) {
00113     if (capacity == 0) {    // initially allocate a data array
00114       if (init_capacity == 0)
00115         init_capacity = INITIAL_PHYSICAL_CAPACITY;
00116 
00117       data = (T*)physical_pages.alloc_page(MEM_CLASS_1MB_TO_16MB,
00118                                            init_capacity * sizeof(T));
00119       capacity = init_capacity;
00120     } else {        // resize the data to be twice as big
00121       data = (T*)physical_pages.resize(data, MEM_CLASS_1MB_TO_16MB,
00122                                        capacity * 2 * sizeof(T));
00123       capacity *= 2;
00124     }
00125   } else {
00126     if (capacity == 0) {    // initially allocate a data array
00127       if (init_capacity == 0)
00128         init_capacity = INITIAL_VIRTUAL_CAPACITY;
00129     
00130       data = (T*)virtual_mem->alloc(init_capacity * sizeof(T));
00131       capacity = INITIAL_VIRTUAL_CAPACITY;
00132     } else {        // resize the data to be twice as big
00133       data = (T*)virtual_mem->resize(data, capacity * 2 * sizeof(T));
00134       capacity *= 2;
00135     }
00136   }
00137 
00138   if (set_size_too)
00139     used_size = capacity;
00140 }
00141 
00142 #endif
00143 
00144 /* Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman
00145  *
00146  * This program is free software; you can redistribute it and/or modify it
00147  * under the terms of the GNU General Public License as published by the
00148  * Free Software Foundation; either version 2 of the License, or (at your
00149  * option) any later version.
00150  * 
00151  * This program is distributed in the hope that it will be useful, but
00152  * WITHOUT ANY WARRANTY; without even the implied warranty of
00153  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00154  * General Public License for more details (in the COPYING file).
00155  * 
00156  * You should have received a copy of the GNU General Public License along
00157  * with this program; if not, write to the Free Software Foundation, Inc.,
00158  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00159  */

Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman