Module: xenomai-forge
Branch: next
Commit: 22321d48f3ede5e64104fb75d1775470038eab91
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=22321d48f3ede5e64104fb75d1775470038eab91

Author: Philippe Gerum <r...@xenomai.org>
Date:   Mon Sep  8 17:58:18 2014 +0200

cobalt/heap: limit all sizes and offsets to 32bit values

The allocator cannot handle more than 2Gb of storage area by design,
and the uapi/ bits already assume that sizes and offsets produced by
Cobalt heaps fit in a 32bit word, for 32/64 ABI neutrality.

Therefore we may turn all relevant long types to u32 in the
implementation, to clearly state the addressing limit of this
allocator.

---

 include/cobalt/kernel/heap.h |   68 ++++++++++++++++++++----------------------
 kernel/cobalt/heap.c         |   22 +++++++-------
 kernel/cobalt/init.c         |    4 +--
 kernel/drivers/ipc/iddp.c    |    2 +-
 kernel/drivers/ipc/xddp.c    |    2 +-
 5 files changed, 47 insertions(+), 51 deletions(-)

diff --git a/include/cobalt/kernel/heap.h b/include/cobalt/kernel/heap.h
index 4bc514e..3f308ec 100644
--- a/include/cobalt/kernel/heap.h
+++ b/include/cobalt/kernel/heap.h
@@ -35,20 +35,17 @@
  *
  * - Maximum page size is 2 ** XNHEAP_MAXLOG2.
  *
- * - Minimum block size equals the minimum page size.
+ * - Requested block size is rounded up to XNHEAP_MINLOG2.
  *
- * - Requested block size smaller than the minimum block size is
- * rounded to the minimum block size.
- *
- * - Requested block size larger than 2 times the page size is rounded
- * to the next page boundary and obtained from the free page list. So
- * we need a bucket for each power of two between XNHEAP_MINLOG2 and
- * XNHEAP_MAXLOG2 inclusive, plus one to honor requests ranging from
- * the maximum page size to twice this size.
+ * - Requested block size larger than 2 times the XNHEAP_PAGESZ is
+ * rounded up to the next page boundary and obtained from the free
+ * page list. So we need a bucket for each power of two between
+ * XNHEAP_MINLOG2 and XNHEAP_MAXLOG2 inclusive, plus one to honor
+ * requests ranging from the maximum page size to twice this size.
  */
 #define XNHEAP_PAGESZ    PAGE_SIZE
 #define XNHEAP_MINLOG2    3
-#define XNHEAP_MAXLOG2    22   /* Must hold pagemap::bcount objects */
+#define XNHEAP_MAXLOG2    22   /* Holds pagemap.bcount blocks */
 #define XNHEAP_MINALLOCSZ (1 << XNHEAP_MINLOG2)
 #define XNHEAP_MINALIGNSZ (1 << 4) /* i.e. 16 bytes */
 #define XNHEAP_NBUCKETS   (XNHEAP_MAXLOG2 - XNHEAP_MINLOG2 + 2)
@@ -59,33 +56,37 @@
 #define XNHEAP_PLIST   2
 
 struct xnpagemap {
-       unsigned int type : 8;    /* PFREE, PCONT, PLIST or log2 */
-       unsigned int bcount : 24; /* Number of active blocks. */
+       /** PFREE, PCONT, PLIST or log2 */
+       u32 type : 8;
+       /** Number of active blocks */
+       u32 bcount : 24;
 };
 
 struct xnheap {
-       char name[XNOBJECT_NAME_LEN];
-       unsigned long size;
-       unsigned long used;
+       /** SMP lock */
        DECLARE_XNLOCK(lock);
-
-       struct xnbucket {
-               caddr_t freelist;
-               int fcount;
-       } buckets[XNHEAP_NBUCKETS];
-
        /** Base address of the page array */
        caddr_t membase;
        /** Memory limit of page array */
        caddr_t memlim;
        /** Number of pages in the freelist */
-       unsigned long npages;
+       int npages;
        /** Head of the free page list */
        caddr_t freelist;
        /** Address of the page map */
        struct xnpagemap *pagemap;
-       /** heapq */
+       /** Link to heapq */
        struct list_head next;
+       /** log2 bucket list */
+       struct xnbucket {
+               caddr_t freelist;
+               int fcount;
+       } buckets[XNHEAP_NBUCKETS];
+       char name[XNOBJECT_NAME_LEN];
+       /** Size of storage area */
+       u32 size;
+       /** Used/busy storage size */
+       u32 used;
 };
 
 extern struct xnheap kheap;
@@ -93,12 +94,12 @@ extern struct xnheap kheap;
 #define xnmalloc(size)     xnheap_alloc(&kheap, size)
 #define xnfree(ptr)        xnheap_free(&kheap, ptr)
 
-static inline size_t xnheap_get_size(const struct xnheap *heap)
+static inline u32 xnheap_get_size(const struct xnheap *heap)
 {
        return heap->size;
 }
 
-static inline size_t xnheap_get_free(const struct xnheap *heap)
+static inline u32 xnheap_get_free(const struct xnheap *heap)
 {
        return heap->size - heap->used;
 }
@@ -108,7 +109,7 @@ static inline void *xnheap_get_membase(const struct xnheap 
*heap)
        return heap->membase;
 }
 
-static inline size_t xnheap_rounded_size(size_t size)
+static inline u32 xnheap_rounded_size(u32 size)
 {
        if (size < 2 * XNHEAP_PAGESZ)
                return 2 * XNHEAP_PAGESZ;
@@ -128,9 +129,7 @@ static inline void xnheap_cleanup_proc(void) { }
 
 /* Public interface. */
 
-int xnheap_init(struct xnheap *heap,
-               void *membase,
-               unsigned long size);
+int xnheap_init(struct xnheap *heap, void *membase, u32 size);
 
 void xnheap_set_name(struct xnheap *heap,
                     const char *name, ...);
@@ -138,18 +137,15 @@ void xnheap_set_name(struct xnheap *heap,
 void xnheap_destroy(struct xnheap *heap,
                    void (*flushfn)(struct xnheap *heap,
                                    void *membase,
-                                   unsigned long size,
+                                   u32 size,
                                    void *cookie),
                    void *cookie);
 
-void *xnheap_alloc(struct xnheap *heap,
-                  unsigned long size);
+void *xnheap_alloc(struct xnheap *heap, u32 size);
 
-void xnheap_free(struct xnheap *heap,
-               void *block);
+void xnheap_free(struct xnheap *heap, void *block);
 
-int xnheap_check_block(struct xnheap *heap,
-                      void *block);
+int xnheap_check_block(struct xnheap *heap, void *block);
 
 /** @} */
 
diff --git a/kernel/cobalt/heap.c b/kernel/cobalt/heap.c
index e32c014..7cb167a 100644
--- a/kernel/cobalt/heap.c
+++ b/kernel/cobalt/heap.c
@@ -164,7 +164,7 @@ static void init_freelist(struct xnheap *heap)
 }
 
 /**
- * @fn xnheap_init(struct xnheap *heap,void *membase,unsigned long size)
+ * @fn xnheap_init(struct xnheap *heap, void *membase, u32 size)
  * @brief Initialize a memory heap.
  *
  * Initializes a memory heap suitable for time-bounded allocation
@@ -191,7 +191,7 @@ static void init_freelist(struct xnheap *heap)
  *
  * @coretags{secondary-only}
  */
-int xnheap_init(struct xnheap *heap, void *membase, unsigned long size)
+int xnheap_init(struct xnheap *heap, void *membase, u32 size)
 {
        spl_t s;
 
@@ -243,7 +243,7 @@ int xnheap_init(struct xnheap *heap, void *membase, 
unsigned long size)
 EXPORT_SYMBOL_GPL(xnheap_init);
 
 /**
- * @fn void xnheap_destroy(struct xnheap *heap, void (*flushfn)(struct xnheap 
*heap, void *membase, unsigned long size, void *cookie), void *cookie)
+ * @fn void xnheap_destroy(struct xnheap *heap, void (*flushfn)(struct xnheap 
*heap, void *membase, u32 size, void *cookie), void *cookie)
  * @brief Destroys a memory heap.
  *
  * Destroys a memory heap.
@@ -261,7 +261,7 @@ EXPORT_SYMBOL_GPL(xnheap_init);
 void xnheap_destroy(struct xnheap *heap,
                    void (*flushfn)(struct xnheap *heap,
                                    void *membase,
-                                   unsigned long size, void *cookie),
+                                   u32 size, void *cookie),
                    void *cookie)
 {
        spl_t s;
@@ -309,10 +309,10 @@ EXPORT_SYMBOL_GPL(xnheap_set_name);
  * acquired the heap lock.
  */
 
-static caddr_t get_free_range(struct xnheap *heap, unsigned long bsize, int 
log2size)
+static caddr_t get_free_range(struct xnheap *heap, u32 bsize, int log2size)
 {
        caddr_t block, eblock, freepage, lastpage, headpage, freehead = NULL;
-       unsigned long pagenum, pagecont, freecont;
+       u32 pagenum, pagecont, freecont;
 
        freepage = heap->freelist;
        while (freepage) {
@@ -393,7 +393,7 @@ splitpage:
 }
 
 /**
- * @fn void *xnheap_alloc(struct xnheap *heap, unsigned long size)
+ * @fn void *xnheap_alloc(struct xnheap *heap, u32 size)
  * @brief Allocate a memory block from a memory heap.
  *
  * Allocates a contiguous region of memory from an active memory heap.
@@ -414,9 +414,9 @@ splitpage:
  *
  * @coretags{unrestricted}
  */
-void *xnheap_alloc(struct xnheap *heap, unsigned long size)
+void *xnheap_alloc(struct xnheap *heap, u32 size)
 {
-       unsigned long pagenum, bsize;
+       u32 pagenum, bsize;
        int log2size, ilog;
        caddr_t block;
        spl_t s;
@@ -503,8 +503,8 @@ EXPORT_SYMBOL_GPL(xnheap_alloc);
 void xnheap_free(struct xnheap *heap, void *block)
 {
        caddr_t freepage, lastpage, nextpage, tailpage, freeptr, *tailptr;
-       unsigned long pagenum, pagecont, boffset, bsize;
        int log2size, npages, nblocks, xpage, ilog;
+       u32 pagenum, pagecont, boffset, bsize;
        spl_t s;
 
        xnlock_get_irqsave(&heap->lock, s);
@@ -644,8 +644,8 @@ EXPORT_SYMBOL_GPL(xnheap_free);
 
 int xnheap_check_block(struct xnheap *heap, void *block)
 {
-       unsigned long pagenum, boffset;
        int ptype, ret = -EINVAL;
+       u32 pagenum, boffset;
        spl_t s;
 
        xnlock_get_irqsave(&heap->lock, s);
diff --git a/kernel/cobalt/init.c b/kernel/cobalt/init.c
index 0defbeb..0c9e08b 100644
--- a/kernel/cobalt/init.c
+++ b/kernel/cobalt/init.c
@@ -117,9 +117,9 @@ static void disable_timesource(void)
 }
 
 static void flush_heap(struct xnheap *heap,
-                      void *extaddr, unsigned long extsize, void *cookie)
+                      void *mem, u32 size, void *cookie)
 {
-       free_pages_exact(extaddr, extsize);
+       free_pages_exact(mem, size);
 }
 
 static void sys_shutdown(void)
diff --git a/kernel/drivers/ipc/iddp.c b/kernel/drivers/ipc/iddp.c
index 0661751..4487bb3 100644
--- a/kernel/drivers/ipc/iddp.c
+++ b/kernel/drivers/ipc/iddp.c
@@ -160,7 +160,7 @@ static void __iddp_free_mbuf(struct iddp_socket *sk,
 }
 
 static void __iddp_flush_pool(struct xnheap *heap,
-                             void *poolmem, u_long poolsz, void *cookie)
+                             void *poolmem, u32 poolsz, void *cookie)
 {
        free_pages_exact(poolmem, poolsz);
 }
diff --git a/kernel/drivers/ipc/xddp.c b/kernel/drivers/ipc/xddp.c
index f86df33..c1be6a5 100644
--- a/kernel/drivers/ipc/xddp.c
+++ b/kernel/drivers/ipc/xddp.c
@@ -105,7 +105,7 @@ static struct xnpnode_link __xddp_pnode = {
 #endif /* !CONFIG_XENO_OPT_VFILE */
 
 static void __xddp_flush_pool(struct xnheap *heap,
-                             void *poolmem, u_long poolsz, void *cookie)
+                             void *poolmem, u32 poolsz, void *cookie)
 {
        free_pages_exact(poolmem, poolsz);
 }


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to