object_heap: Sanitize coding style

Signed-off-by: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
This commit is contained in:
Paul Kocialkowski
2018-06-22 09:58:45 +02:00
parent e23807f928
commit 000ca7239a
2 changed files with 119 additions and 174 deletions
+97 -132
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007 Intel Corporation. All Rights Reserved. * Copyright (C) 2007 Intel Corporation. All Rights Reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the * copy of this software and associated documentation files (the
@@ -23,233 +23,198 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include "object_heap.h" #include "object_heap.h"
#define ASSERT assert static int object_heap_expand(struct object_heap *heap)
#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)
{ {
int i; struct object_base *object;
void *new_heap_index; void *new_heap_index;
int next_free;
int new_heap_size = heap->heap_size + heap->heap_increment; int new_heap_size = heap->heap_size + heap->heap_increment;
int bucket_index = new_heap_size / heap->heap_increment - 1; int bucket_index = new_heap_size / heap->heap_increment - 1;
int next_free;
int i;
if (bucket_index >= heap->num_buckets) { if (bucket_index >= heap->num_buckets) {
int new_num_buckets = heap->num_buckets + 8; int new_num_buckets = heap->num_buckets + 8;
void **new_bucket; void **new_bucket;
new_bucket = realloc(heap->bucket, new_num_buckets * sizeof(void *)); new_bucket = realloc(heap->bucket, new_num_buckets * sizeof(void *));
if (NULL == new_bucket) { if (new_bucket == NULL)
return -1; return -1;
}
heap->num_buckets = new_num_buckets; heap->num_buckets = new_num_buckets;
heap->bucket = new_bucket; heap->bucket = new_bucket;
} }
new_heap_index = (void *) malloc(heap->heap_increment * heap->object_size); new_heap_index = malloc(heap->heap_increment * heap->object_size);
if (NULL == new_heap_index) { if (new_heap_index == NULL)
return -1; /* Out of memory */ return -1;
}
heap->bucket[bucket_index] = new_heap_index; heap->bucket[bucket_index] = new_heap_index;
next_free = heap->next_free; next_free = heap->next_free;
for (i = new_heap_size; i-- > heap->heap_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); object = (struct object_base *)(new_heap_index + (i - heap->heap_size) * heap->object_size);
obj->id = i + heap->id_offset; object->id = i + heap->id_offset;
obj->next_free = next_free; object->next_free = next_free;
next_free = i; next_free = i;
} }
heap->next_free = next_free; heap->next_free = next_free;
heap->heap_size = new_heap_size; heap->heap_size = new_heap_size;
return 0; /* Success */
return 0;
} }
/* static int object_heap_allocate_unlocked(struct object_heap *heap)
* Return 0 on success, -1 on error {
*/ struct object_base *object;
int object_heap_init(object_heap_p heap, int object_size, int id_offset) int bucket_index, object_index;
if (heap->next_free == OBJECT_HEAP_LAST)
if (object_heap_expand(heap) == -1)
return -1;
if (heap->next_free < 0)
return -1;
bucket_index = heap->next_free / heap->heap_increment;
object_index = heap->next_free % heap->heap_increment;
object = (struct object_base *) (heap->bucket[bucket_index] + object_index * heap->object_size);
heap->next_free = object->next_free;
object->next_free = OBJECT_HEAP_ALLOCATED;
return object->id;
}
int object_heap_init(struct object_heap *heap, int object_size, int id_offset)
{ {
pthread_mutex_init(&heap->mutex, NULL); pthread_mutex_init(&heap->mutex, NULL);
heap->object_size = object_size; heap->object_size = object_size;
heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK; heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK;
heap->heap_size = 0; heap->heap_size = 0;
heap->heap_increment = 16; heap->heap_increment = 16;
heap->next_free = LAST_FREE; heap->next_free = OBJECT_HEAP_LAST;
heap->num_buckets = 0; heap->num_buckets = 0;
heap->bucket = NULL; heap->bucket = NULL;
return object_heap_expand(heap); return object_heap_expand(heap);
} }
/* int object_heap_allocate(struct object_heap *heap)
* Allocates an object
* Returns the object ID on success, returns -1 on error
*/
static int object_heap_allocate_unlocked(object_heap_p heap)
{ {
object_base_p obj; int rc;
int bucket_index, obj_index;
if (LAST_FREE == heap->next_free) {
if (-1 == object_heap_expand(heap)) {
return -1; /* Out of memory */
}
}
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);
heap->next_free = obj->next_free;
obj->next_free = ALLOCATED;
return obj->id;
}
int object_heap_allocate(object_heap_p heap)
{
int ret;
pthread_mutex_lock(&heap->mutex); pthread_mutex_lock(&heap->mutex);
ret = object_heap_allocate_unlocked(heap); rc = object_heap_allocate_unlocked(heap);
pthread_mutex_unlock(&heap->mutex); pthread_mutex_unlock(&heap->mutex);
return ret;
return rc;
} }
/* static struct object_base *object_heap_lookup_unlocked(struct object_heap *heap, int id)
* 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)
{ {
object_base_p obj; struct object_base *object;
int bucket_index, obj_index; int bucket_index, object_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; return NULL;
}
id &= OBJECT_HEAP_ID_MASK; id &= OBJECT_HEAP_ID_MASK;
bucket_index = id / heap->heap_increment; bucket_index = id / heap->heap_increment;
obj_index = id % heap->heap_increment; object_index = id % heap->heap_increment;
obj = (object_base_p)(heap->bucket[bucket_index] + obj_index * heap->object_size);
/* Check if the object has in fact been allocated */ object = (struct object_base *)(heap->bucket[bucket_index] + object_index * heap->object_size);
if (obj->next_free != ALLOCATED) {
if (object->next_free != OBJECT_HEAP_ALLOCATED)
return NULL; return NULL;
}
return obj; return object;
} }
object_base_p object_heap_lookup(object_heap_p heap, int id) struct object_base *object_heap_lookup(struct object_heap *heap, int id)
{ {
object_base_p obj; struct object_base *object;
pthread_mutex_lock(&heap->mutex); pthread_mutex_lock(&heap->mutex);
obj = object_heap_lookup_unlocked(heap, id); object = object_heap_lookup_unlocked(heap, id);
pthread_mutex_unlock(&heap->mutex); pthread_mutex_unlock(&heap->mutex);
return obj;
return object;
} }
/* struct object_base *object_heap_first(struct object_heap *heap, int *iterator)
* 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)
{ {
*iter = -1; *iterator = -1;
return object_heap_next(heap, iter);
return object_heap_next(heap, iterator);
} }
/* static struct object_base *object_heap_next_unlocked(struct object_heap *heap, int *iterator)
* Iterate over all objects in the heap.
* 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_base_p obj; struct object_base *object;
int bucket_index, obj_index; int bucket_index, object_index;
int i = *iter + 1; int i = *iterator + 1;
while (i < heap->heap_size) { while (i < heap->heap_size) {
bucket_index = i / heap->heap_increment; bucket_index = i / heap->heap_increment;
obj_index = i % heap->heap_increment; object_index = i % heap->heap_increment;
obj = (object_base_p)(heap->bucket[bucket_index] + obj_index * heap->object_size); object = (struct object_base *)(heap->bucket[bucket_index] + object_index * heap->object_size);
if (obj->next_free == ALLOCATED) { if (object->next_free == OBJECT_HEAP_ALLOCATED) {
*iter = i; *iterator = i;
return obj; return object;
} }
i++; i++;
} }
*iter = i;
*iterator = i;
return NULL; return NULL;
} }
object_base_p object_heap_next(object_heap_p heap, object_heap_iterator *iter) struct object_base *object_heap_next(struct object_heap *heap, int *iterator)
{ {
object_base_p obj; struct object_base *object;
pthread_mutex_lock(&heap->mutex); pthread_mutex_lock(&heap->mutex);
obj = object_heap_next_unlocked(heap, iter); object = object_heap_next_unlocked(heap, iterator);
pthread_mutex_unlock(&heap->mutex); pthread_mutex_unlock(&heap->mutex);
return obj;
return object;
} }
/* static void object_heap_free_unlocked(struct object_heap *heap, struct object_base *object)
* Frees an object
*/
static void object_heap_free_unlocked(object_heap_p heap, object_base_p obj)
{ {
/* Check if the object has in fact been allocated */ object->next_free = heap->next_free;
ASSERT(obj->next_free == ALLOCATED); heap->next_free = object->id & OBJECT_HEAP_ID_MASK;
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) void object_heap_free(struct object_heap *heap, struct object_base *object)
{ {
if (!obj) if (!object)
return; return;
pthread_mutex_lock(&heap->mutex); pthread_mutex_lock(&heap->mutex);
object_heap_free_unlocked(heap, obj); object_heap_free_unlocked(heap, object);
pthread_mutex_unlock(&heap->mutex); pthread_mutex_unlock(&heap->mutex);
} }
/* void object_heap_destroy(struct object_heap *heap)
* Destroys a heap, the heap must be empty.
*/
void object_heap_destroy(object_heap_p heap)
{ {
object_base_p obj; int i;
int bucket_index, obj_index, i;
/* Check if heap is empty */ for (i = 0; i < heap->heap_size / heap->heap_increment; 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);
}
for (i = 0; i < heap->heap_size / heap->heap_increment; i++) {
free(heap->bucket[i]); free(heap->bucket[i]);
}
pthread_mutex_destroy(&heap->mutex); pthread_mutex_destroy(&heap->mutex);
free(heap->bucket); free(heap->bucket);
heap->bucket = NULL; heap->bucket = NULL;
heap->heap_size = 0; heap->heap_size = 0;
heap->next_free = LAST_FREE; heap->next_free = OBJECT_HEAP_LAST;
} }
+22 -42
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007 Intel Corporation. All Rights Reserved. * Copyright (C) 2007 Intel Corporation. All Rights Reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the * copy of this software and associated documentation files (the
@@ -27,11 +27,20 @@
#include <pthread.h> #include <pthread.h>
#define OBJECT_HEAP_OFFSET_MASK 0x7F000000 /*
#define OBJECT_HEAP_ID_MASK 0x00FFFFFF * Values
*/
#define OBJECT_HEAP_OFFSET_MASK 0x7F000000
#define OBJECT_HEAP_ID_MASK 0x00FFFFFF
#define OBJECT_HEAP_LAST -1
#define OBJECT_HEAP_ALLOCATED -2
/*
* Structures
*/
typedef struct object_base *object_base_p;
typedef struct object_heap *object_heap_p;
struct object_base { struct object_base {
int id; int id;
@@ -49,45 +58,16 @@ struct object_heap {
int num_buckets; int num_buckets;
}; };
typedef int object_heap_iterator;
/* /*
* Return 0 on success, -1 on error * Functions
*/ */
int object_heap_init(object_heap_p heap, int object_size, int id_offset);
/* int object_heap_init(struct object_heap *heap, int object_size, int id_offset);
* Allocates an object int object_heap_allocate(struct object_heap *heap);
* Returns the object ID on success, returns -1 on error struct object_base *object_heap_lookup(struct object_heap *heap, int id);
*/ struct object_base *object_heap_first(struct object_heap *heap, int *iterator);
int object_heap_allocate(object_heap_p heap); struct object_base *object_heap_next(struct object_heap *heap, int *iterator);
void object_heap_free(struct object_heap *heap, struct object_base *object);
/* void object_heap_destroy(struct object_heap *heap);
* 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);
/*
* 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);
/*
* 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);
/*
* Frees an object
*/
void 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);
#endif #endif