summaryrefslogtreecommitdiff
path: root/cesar/lib
diff options
context:
space:
mode:
authorYacine Belkadi2011-10-03 18:23:53 +0200
committerYacine Belkadi2011-11-02 14:55:28 +0100
commit8ff4e4b62152f62ce020930b5962f46a78761813 (patch)
treeaa612fe1a3081f7837f6917ecfdce337faf77d6d /cesar/lib
parentfa2364ddbe779d12697d7eb5636f4bac53411e8b (diff)
cesar/lib/blk: pass block memory size via blk_init(), refs #583
lib/blk uses CONFIG_NB_BLK to determine the size of memory it can allocate. The present commit adds the possibility to pass that size of memory via blk_init(). This will allow to set that size dynamically on startup, based on the total memory size.
Diffstat (limited to 'cesar/lib')
-rw-r--r--cesar/lib/blk.h4
-rw-r--r--cesar/lib/src/blk.c26
2 files changed, 23 insertions, 7 deletions
diff --git a/cesar/lib/blk.h b/cesar/lib/blk.h
index 44277badca..629e7c2ef0 100644
--- a/cesar/lib/blk.h
+++ b/cesar/lib/blk.h
@@ -74,9 +74,11 @@ BEGIN_DECLS
/**
* Initialise the block allocator.
+ * \param blk_mem_size size of the memory that can be used for block
+ * allocation
*/
void
-blk_init (void);
+blk_init (uint blk_mem_size);
/**
* Return a newly allocated 512 byte block with its descriptor.
diff --git a/cesar/lib/src/blk.c b/cesar/lib/src/blk.c
index 445339eb12..da596756de 100644
--- a/cesar/lib/src/blk.c
+++ b/cesar/lib/src/blk.c
@@ -68,6 +68,9 @@
/** Block allocator context. */
struct blk_global_t
{
+ /** Size of the memory for blocks allocation.
+ * Relies on automatic initialization to be set to zero. */
+ uint blk_mem_size;
/** Offset to compute reference counter address from descriptor
* address. */
uint desc_to_refcnt_offset;
@@ -126,15 +129,26 @@ struct blk_obj_t
typedef struct blk_obj_t blk_obj_t;
void
-blk_init (void)
+blk_init (uint blk_mem_size)
{
+ blk_global.blk_mem_size = blk_mem_size;
+}
+
+/**
+ * Real Init.
+ */
+static void
+blk_do_init (void)
+{
+ /* If the blk memory size is unknown, use the value of CONFIG_BLK_NB. */
+ if (blk_global.blk_mem_size == 0)
+ blk_global.blk_mem_size = BLK_TOTAL_SIZE * (CONFIG_BLK_NB + 1);
/* Allocate memory. */
- uint size = BLK_TOTAL_SIZE * (CONFIG_BLK_NB + 1);
- u8 *alloc = malloc (size);
+ u8 *alloc = malloc (blk_global.blk_mem_size);
if (!alloc)
dbg_fatal ("exhausted virtual memory");
uint base = (uint) ARCH_CPU_TO_DMA (alloc);
- uint start = base, stop = base + size;
+ uint start = base, stop = base + blk_global.blk_mem_size;
/* Align block, descriptors and reference counters. */
stop = stop & ~(BLK_SIZE - 1);
uint nb_blks = (stop - start) / BLK_TOTAL_SIZE;
@@ -182,7 +196,7 @@ blk_alloc_desc_ (void_FL)
{
/* Initialise block allocator. */
if (!blk_global.inited)
- blk_init ();
+ blk_do_init ();
/* Get a block from free list. */
uint flags = arch_isr_lock ();
blk_t *blk = blk_global.free_list;
@@ -209,7 +223,7 @@ blk_alloc_desc_range_ (uint n, blk_t **last __FL)
dbg_blame_ptr (last);
/* Initialise block allocator. */
if (!blk_global.inited)
- blk_init ();
+ blk_do_init ();
/* Get blocks from free list. */
left = n;
pfirst = &first;