CObject
CCArrayList.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Brendan Bruner
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * bbruner@ualberta.ca
17  * July 3, 2016
18  */
22 #ifndef UTIL_CIARRAYLIST_H_
23 #define UTIL_CIARRAYLIST_H_
24 
25 #include "CIList.h"
26 #include <CError.h>
27 
28 
29 /* There is one bit for every element in the list do indicate full/empty status.
30  * To get the number of bytes required for a one to one
31  * map of element indices to bits: divide the max size of the list by the
32  * number of bits in a byte, then add one to ensure
33  * a round up.
34  */
35 #define CCARRAY_LIST_MASK_SIZE( list_size ) (((list_size) >> 3) + 1)
36 
37 /* Macro which resolves to the number of bytes that must be
38  * allocated for a list of length 'list_length' with elements of size
39  * 'element_length'.
40  */
41 #define CCARRAY_LIST_SIZE( element_length, list_length ) \
42  (((list_length) * (element_length)) + CCARRAY_LIST_MASK_SIZE(list_length))
43 
44 
45 /************************************************************************/
46 /* Class and vtable decalre. */
47 /************************************************************************/
93 {
94  /* Super class must always be first member */
95  /* of a C class struct. */
96  struct CObject cobject;
97 
98  /* Implementing the CIList interface. */
99  struct CIList cilist;
100 
101  /* Private member variables. */
102  struct
103  {
104  /* Base address of memory used for the lsit
105  */
106  char* list_base;
107 
108  /* Defines a bit mask to indicate the empty/full
109  * status of all list elements. For example, bit 3
110  * indicates if the element at index 3 is empty/full.
111  */
112  unsigned char* list_mask;
113 
114  size_t max_size;
115  size_t current_size;
116  size_t element_size;
117 
118  /* Holds the last known empty index within the list.
119  * This is a cache to speed up the CIList_Add( )
120  * implementation.
121  */
122  size_t add_index;
123 
124  /* Used in destructor.
125  */
126  CBool is_static;
127  } _;
128 };
129 
136 {
137  /* Space for a copy of the super class' virtual table must */
138  /* be the first member of a class virtual table declaration. */
139  struct CObject_VTable cobject_override;
140 
141  /* Since we are overriding the destructor, we need to keep */
142  /* keep a reference to the super class' implementation of */
143  /* the destructor. */
144  const struct CObject_VTable* cobject;
145 
146  /* Space for a copy of the implemented interface's virtual table */
147  struct CIList_VTable cilist_override;
148 };
149 
157 
158 
159 /************************************************************************/
160 /* Constructor */
161 /************************************************************************/
182 CError CCArrayList( struct CCArrayList* self, size_t element_size, size_t max_size );
183 
208 CError CCArrayListStatic
209 (
210  struct CCArrayList* self,
211  size_t element_size,
212  size_t max_size,
213  void* memory
214 );
215 
216 #endif /* UTIL_CIARRAYLIST_H_ */
217 
CIList virtual table.
Definition: CIList.h:81
CCArrayList&#39;s vtable declaration.
Definition: CCArrayList.h:135
Interface for list data structures.
Definition: CIList.h:69
CError CCArrayListStatic(struct CCArrayList *self, size_t element_size, size_t max_size, void *memory)
Definition: CCArrayList.c:425
Base class.
Definition: Class.h:283
CError CCArrayList(struct CCArrayList *self, size_t element_size, size_t max_size)
Definition: CCArrayList.c:374
CObject&#39;s virtual table declaration.
Definition: Class.h:306
Array implementation of CIList.
Definition: CCArrayList.h:92
const struct CCArrayList_VTable * CCArrayList_GetVTable()
Definition: CCArrayList.c:344