aboutsummaryrefslogtreecommitdiff
path: root/core/pool.c
diff options
context:
space:
mode:
authorAlistair Popple <alistair@popple.id.au>2014-10-20 11:42:54 +1100
committerStewart Smith <stewart@linux.vnet.ibm.com>2014-10-22 18:00:10 +1100
commitcf6f4e8912d29fb89ce85c84834607065ad595a5 (patch)
tree4649ba458e1a9e1726135304ac135320b612d268 /core/pool.c
parentde6c2a5ad4baf0a45e72ba82d3ba87446e68768c (diff)
downloadskiboot-cf6f4e8912d29fb89ce85c84834607065ad595a5.zip
skiboot-cf6f4e8912d29fb89ce85c84834607065ad595a5.tar.gz
skiboot-cf6f4e8912d29fb89ce85c84834607065ad595a5.tar.bz2
fsp/elog: Create a logging frontend
In order to support fsp-less machines we need to be able to log errors using a BMC or some other mechanism. Currently the error logging code is tightly coupled to the platform making it difficult to add different platforms. This patch factors out the generic parts of the error logging code in preparation for adding different logging backends. It also adds a generic mechanism for pre-allocating a specific number of objects. Signed-off-by: Alistair Popple <alistair@popple.id.au> Signed-off-by: Stewart Smith <stewart@linux.vnet.ibm.com>
Diffstat (limited to 'core/pool.c')
-rw-r--r--core/pool.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/core/pool.c b/core/pool.c
new file mode 100644
index 0000000..adf6c90
--- /dev/null
+++ b/core/pool.c
@@ -0,0 +1,80 @@
+/* Copyright 2013-2014 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This file provides some functions to manage a pool of pre-allocated
+ * objects. It also provides a method to reserve a pre-defined number
+ * of objects for higher priorty requests. The allocations follow the
+ * following rules:
+ *
+ * 1. An allocation will succeed at any priority if there is more than
+ * the reserved number of objects free.
+ * 2. Only high priority allocations will succeed when there are less
+ * than the reserved number of objects free.
+ * 3. When an allocation is freed it is always added to the high priority
+ * pool if there are less than the reserved number of allocations
+ * available.
+ */
+
+#include <pool.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ccan/list/list.h>
+
+void* pool_get(struct pool *pool, enum pool_priority priority)
+{
+ void *obj;
+
+ if (!pool->free_count ||
+ ((pool->free_count <= pool->reserved) && priority == POOL_NORMAL))
+ return NULL;
+
+ pool->free_count--;
+ obj = (void *) list_pop_(&pool->free_list, 0);
+ assert(obj);
+ memset(obj, 0, pool->obj_size);
+ return obj;
+}
+
+void pool_free_object(struct pool *pool, void *obj)
+{
+ pool->free_count++;
+ list_add_tail(&pool->free_list,
+ (struct list_node *) (obj));
+}
+
+int pool_init(struct pool *pool, size_t obj_size, int count, int reserved)
+{
+ int i;
+
+ if (obj_size < sizeof(struct list_node))
+ obj_size = sizeof(struct list_node);
+
+ assert(count >= reserved);
+ pool->buf = malloc(obj_size*count);
+ if (!pool->buf)
+ return -1;
+
+ pool->obj_size = obj_size;
+ pool->free_count = count;
+ pool->reserved = reserved;
+ list_head_init(&pool->free_list);
+
+ for(i = 0; i < count; i++)
+ list_add_tail(&pool->free_list,
+ (struct list_node *) (pool->buf + obj_size*i));
+
+ return 0;
+}