summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGwenole Beauchesne <gwenole.beauchesne@intel.com>2012-09-28 14:41:25 +0200
committerGwenole Beauchesne <gwenole.beauchesne@intel.com>2012-09-28 14:41:25 +0200
commit9c968f8b9c53d308e3bfd796a1915096f6ab5ea3 (patch)
treeeba2dc41fc2ba5a59067959b58091015526b4276
parent0e0725cb9d139bf42190d3572dc6350cedbfb22f (diff)
objects: sanitize coding style.
-rw-r--r--src/object_heap.c91
-rw-r--r--src/object_heap.h38
2 files changed, 67 insertions, 62 deletions
diff --git a/src/object_heap.c b/src/object_heap.c
index 615cd38..dcc4a75 100644
--- a/src/object_heap.c
+++ b/src/object_heap.c
@@ -32,11 +32,11 @@
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
- *
+ *
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
@@ -46,14 +46,15 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
-#define LAST_FREE -1
-#define ALLOCATED -2
+#define LAST_FREE -1
+#define ALLOCATED -2
/*
* Expands the heap
* Return 0 on success, -1 on error
*/
-static int object_heap_expand( object_heap_p heap )
+static int
+object_heap_expand(object_heap_p heap)
{
int i;
void *new_heap_index;
@@ -74,17 +75,15 @@ static int object_heap_expand( object_heap_p heap )
heap->bucket = new_bucket;
}
- new_heap_index = (void *) malloc( heap->heap_increment * heap->object_size );
- if ( NULL == new_heap_index )
- {
+ new_heap_index = (void *) malloc(heap->heap_increment * heap->object_size);
+ if (NULL == new_heap_index) {
return -1; /* Out of memory */
}
heap->bucket[bucket_index] = new_heap_index;
next_free = heap->next_free;
- for(i = new_heap_size; i-- > heap->heap_size; )
- {
- object_base_p obj = (object_base_p) (new_heap_index + (i - heap->heap_size) * heap->object_size);
+ for (i = new_heap_size; i-- > heap->heap_size;) {
+ object_base_p obj = (object_base_p)(new_heap_index + (i - heap->heap_size) * heap->object_size);
obj->id = i + heap->id_offset;
obj->next_free = next_free;
next_free = i;
@@ -97,7 +96,8 @@ static int object_heap_expand( object_heap_p heap )
/*
* Return 0 on success, -1 on error
*/
-int object_heap_init( object_heap_p heap, int object_size, int id_offset)
+int
+object_heap_init(object_heap_p heap, int object_size, int id_offset)
{
pthread_mutex_init(&heap->mutex, NULL);
heap->object_size = object_size;
@@ -114,30 +114,30 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset)
* Allocates an object
* Returns the object ID on success, returns -1 on error
*/
-static int object_heap_allocate_unlocked( object_heap_p heap )
+static int
+object_heap_allocate_unlocked(object_heap_p heap)
{
object_base_p obj;
int bucket_index, obj_index;
- if ( LAST_FREE == heap->next_free )
- {
- if( -1 == object_heap_expand( heap ) )
- {
+ if (LAST_FREE == heap->next_free) {
+ if (-1 == object_heap_expand(heap)) {
return -1; /* Out of memory */
}
}
- ASSERT( heap->next_free >= 0 );
+ ASSERT(heap->next_free >= 0);
bucket_index = heap->next_free / heap->heap_increment;
obj_index = heap->next_free % heap->heap_increment;
- obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
+ obj = (object_base_p)(heap->bucket[bucket_index] + obj_index * heap->object_size);
heap->next_free = obj->next_free;
obj->next_free = ALLOCATED;
return obj->id;
}
-int object_heap_allocate( object_heap_p heap )
+int
+object_heap_allocate(object_heap_p heap)
{
int ret;
@@ -151,29 +151,29 @@ int object_heap_allocate( object_heap_p heap )
* Lookup an object by object ID
* Returns a pointer to the object on success, returns NULL on error
*/
-static object_base_p object_heap_lookup_unlocked( object_heap_p heap, int id )
+static object_base_p
+object_heap_lookup_unlocked(object_heap_p heap, int id)
{
object_base_p obj;
int bucket_index, obj_index;
- if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
- {
+ if ((id < heap->id_offset) || (id > (heap->heap_size + heap->id_offset))) {
return NULL;
}
id &= OBJECT_HEAP_ID_MASK;
bucket_index = id / heap->heap_increment;
obj_index = id % heap->heap_increment;
- obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
+ obj = (object_base_p)(heap->bucket[bucket_index] + obj_index * heap->object_size);
- /* Check if the object has in fact been allocated */
- if ( obj->next_free != ALLOCATED )
- {
+ /* Check if the object has in fact been allocated */
+ if (obj->next_free != ALLOCATED) {
return NULL;
}
return obj;
}
-object_base_p object_heap_lookup( object_heap_p heap, int id )
+object_base_p
+object_heap_lookup(object_heap_p heap, int id)
{
object_base_p obj;
@@ -187,10 +187,11 @@ object_base_p object_heap_lookup( object_heap_p heap, int id )
* Iterate over all objects in the heap.
* Returns a pointer to the first object on the heap, returns NULL if heap is empty.
*/
-object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter )
+object_base_p
+object_heap_first(object_heap_p heap, object_heap_iterator *iter)
{
*iter = -1;
- return object_heap_next( heap, iter );
+ return object_heap_next(heap, iter);
}
/*
@@ -198,20 +199,18 @@ object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter
* Returns a pointer to the next object on the heap, returns NULL if heap is empty.
*/
static object_base_p
-object_heap_next_unlocked( object_heap_p heap, object_heap_iterator *iter )
+object_heap_next_unlocked(object_heap_p heap, object_heap_iterator *iter)
{
object_base_p obj;
int bucket_index, obj_index;
int i = *iter + 1;
- while ( i < heap->heap_size)
- {
+ while (i < heap->heap_size) {
bucket_index = i / heap->heap_increment;
obj_index = i % heap->heap_increment;
- obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
- if (obj->next_free == ALLOCATED)
- {
+ obj = (object_base_p)(heap->bucket[bucket_index] + obj_index * heap->object_size);
+ if (obj->next_free == ALLOCATED) {
*iter = i;
return obj;
}
@@ -222,7 +221,7 @@ object_heap_next_unlocked( object_heap_p heap, object_heap_iterator *iter )
}
object_base_p
-object_heap_next( object_heap_p heap, object_heap_iterator *iter )
+object_heap_next(object_heap_p heap, object_heap_iterator *iter)
{
object_base_p obj;
@@ -236,17 +235,17 @@ object_heap_next( object_heap_p heap, object_heap_iterator *iter )
* Frees an object
*/
static void
-object_heap_free_unlocked( object_heap_p heap, object_base_p obj )
+object_heap_free_unlocked(object_heap_p heap, object_base_p obj)
{
/* Check if the object has in fact been allocated */
- ASSERT( obj->next_free == ALLOCATED );
-
+ ASSERT(obj->next_free == ALLOCATED);
+
obj->next_free = heap->next_free;
heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
}
void
-object_heap_free( object_heap_p heap, object_base_p obj )
+object_heap_free(object_heap_p heap, object_base_p obj)
{
if (!obj)
return;
@@ -258,19 +257,19 @@ object_heap_free( object_heap_p heap, object_base_p obj )
/*
* Destroys a heap, the heap must be empty.
*/
-void object_heap_destroy( object_heap_p heap )
+void
+object_heap_destroy(object_heap_p heap)
{
object_base_p obj;
int bucket_index, obj_index, i;
/* Check if heap is empty */
- for (i = 0; i < heap->heap_size; i++)
- {
+ for (i = 0; i < heap->heap_size; i++) {
/* Check if object is not still allocated */
bucket_index = i / heap->heap_increment;
obj_index = i % heap->heap_increment;
- obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
- ASSERT( obj->next_free != ALLOCATED );
+ obj = (object_base_p)(heap->bucket[bucket_index] + obj_index * heap->object_size);
+ ASSERT(obj->next_free != ALLOCATED);
}
for (i = 0; i < heap->heap_size / heap->heap_increment; i++) {
diff --git a/src/object_heap.h b/src/object_heap.h
index e3301b4..1e2daca 100644
--- a/src/object_heap.h
+++ b/src/object_heap.h
@@ -21,8 +21,8 @@
#ifndef VA_OBJECT_HEAP_H
#define VA_OBJECT_HEAP_H
-#define OBJECT_HEAP_OFFSET_MASK 0x7f000000
-#define OBJECT_HEAP_ID_MASK 0x00ffffff
+#define OBJECT_HEAP_OFFSET_MASK 0x7f000000
+#define OBJECT_HEAP_ID_MASK 0x00ffffff
typedef struct object_base *object_base_p;
typedef struct object_heap *object_heap_p;
@@ -34,7 +34,7 @@ struct object_base {
struct object_heap {
pthread_mutex_t mutex;
- int object_size;
+ int object_size;
int id_offset;
int next_free;
int heap_size;
@@ -48,47 +48,53 @@ typedef int object_heap_iterator;
/*
* Return 0 on success, -1 on error
*/
-int object_heap_init(object_heap_p heap, int object_size, int id_offset)
- attribute_hidden;
+int
+object_heap_init(object_heap_p heap, int object_size, int id_offset)
+ attribute_hidden;
/*
* Allocates an object
* Returns the object ID on success, returns -1 on error
*/
int object_heap_allocate(object_heap_p heap)
- attribute_hidden;
+ attribute_hidden;
/*
* Lookup an allocated object by object ID
* Returns a pointer to the object on success, returns NULL on error
*/
-object_base_p object_heap_lookup(object_heap_p heap, int id)
- attribute_hidden;
+object_base_p
+object_heap_lookup(object_heap_p heap, int id)
+ attribute_hidden;
/*
* Iterate over all objects in the heap.
* Returns a pointer to the first object on the heap, returns NULL if heap is empty.
*/
-object_base_p object_heap_first(object_heap_p heap, object_heap_iterator *iter)
- attribute_hidden;
+object_base_p
+object_heap_first(object_heap_p heap, object_heap_iterator *iter)
+ attribute_hidden;
/*
* Iterate over all objects in the heap.
* Returns a pointer to the next object on the heap, returns NULL if heap is empty.
*/
-object_base_p object_heap_next(object_heap_p heap, object_heap_iterator *iter)
- attribute_hidden;
+object_base_p
+object_heap_next(object_heap_p heap, object_heap_iterator *iter)
+ attribute_hidden;
/*
* Frees an object
*/
-void object_heap_free(object_heap_p heap, object_base_p obj)
- attribute_hidden;
+void
+object_heap_free(object_heap_p heap, object_base_p obj)
+ attribute_hidden;
/*
* Destroys a heap, the heap must be empty.
*/
-void object_heap_destroy(object_heap_p heap)
- attribute_hidden;
+void
+object_heap_destroy(object_heap_p heap)
+ attribute_hidden;
#endif /* VA_OBJECT_HEAP_H */