Module: xenomai-3
Branch: next
Commit: 54eb6792cedea0054df16735e3639d296104486e
URL:    
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=54eb6792cedea0054df16735e3639d296104486e

Author: Philippe Gerum <r...@xenomai.org>
Date:   Fri Jun 19 12:36:52 2015 +0200

copperplate/heapobj: fix references to shared pool

---

 include/boilerplate/scope.h       |    2 +-
 include/copperplate/heapobj.h     |   27 ++++++++++++++++----------
 include/copperplate/threadobj.h   |    2 +-
 lib/copperplate/heapobj-malloc.c  |   12 ++++++------
 lib/copperplate/heapobj-pshared.c |   38 ++++++++++++++++++++-----------------
 lib/copperplate/heapobj-tlsf.c    |    2 +-
 6 files changed, 47 insertions(+), 36 deletions(-)

diff --git a/include/boilerplate/scope.h b/include/boilerplate/scope.h
index ae71063..d2203b3 100644
--- a/include/boilerplate/scope.h
+++ b/include/boilerplate/scope.h
@@ -34,7 +34,7 @@ int pshared_check(void *heap, void *addr);
 #define dref_type(t)   memoff_t
 
 #define __memoff(__base, __addr)       ((caddr_t)(__addr) - (caddr_t)(__base))
-#define __memptr(__base, __off)                ((caddr_t)(__base) + (__off))
+#define __memptr(__base, __off)                ((void *)((caddr_t)(__base) + 
(__off)))
 #define __memchk(__base, __addr)       pshared_check(__base, __addr)
 
 #define __moff(__p)    __memoff(__main_heap, __p)
diff --git a/include/copperplate/heapobj.h b/include/copperplate/heapobj.h
index 0e70843..682848e 100644
--- a/include/copperplate/heapobj.h
+++ b/include/copperplate/heapobj.h
@@ -33,12 +33,19 @@
 #include <copperplate/debug.h>
 
 struct heapobj {
-       void *pool;
-       size_t size;
-       char name[32];
+       union {
 #ifdef CONFIG_XENO_PSHARED
-       char fsname[256];
+               struct {
+                       dref_type(void *) pool;
+                       char fsname[256];
+               } shared;
 #endif
+               struct {
+                       void *pool;
+               } private;
+       };
+       size_t size;
+       char name[32];
 };
 
 struct sysgroup {
@@ -78,13 +85,13 @@ size_t malloc_usable_size_ex(void *ptr, void *pool);
 static inline
 void pvheapobj_destroy(struct heapobj *hobj)
 {
-       destroy_memory_pool(hobj->pool);
+       destroy_memory_pool(hobj->private.pool);
 }
 
 static inline
 int pvheapobj_extend(struct heapobj *hobj, size_t size, void *mem)
 {
-       hobj->size = add_new_area(hobj->pool, size, mem);
+       hobj->size = add_new_area(hobj->private.pool, size, mem);
        if (hobj->size == (size_t)-1)
                return __bt(-EINVAL);
 
@@ -94,25 +101,25 @@ int pvheapobj_extend(struct heapobj *hobj, size_t size, 
void *mem)
 static inline
 void *pvheapobj_alloc(struct heapobj *hobj, size_t size)
 {
-       return malloc_ex(size, hobj->pool);
+       return malloc_ex(size, hobj->private.pool);
 }
 
 static inline
 void pvheapobj_free(struct heapobj *hobj, void *ptr)
 {
-       free_ex(ptr, hobj->pool);
+       free_ex(ptr, hobj->private.pool);
 }
 
 static inline
 size_t pvheapobj_validate(struct heapobj *hobj, void *ptr)
 {
-       return malloc_usable_size_ex(ptr, hobj->pool);
+       return malloc_usable_size_ex(ptr, hobj->private.pool);
 }
 
 static inline
 size_t pvheapobj_inquire(struct heapobj *hobj)
 {
-       return get_used_size(hobj->pool);
+       return get_used_size(hobj->private.pool);
 }
 
 static inline void *pvmalloc(size_t size)
diff --git a/include/copperplate/threadobj.h b/include/copperplate/threadobj.h
index 4562dcb..aa2c201 100644
--- a/include/copperplate/threadobj.h
+++ b/include/copperplate/threadobj.h
@@ -496,7 +496,7 @@ static inline int threadobj_get_errno(struct threadobj 
*thobj)
                struct threadobj *__thobj = threadobj_current();        \
                assert(__thobj != NULL);                                \
                assert(sizeof(typeof(T)) <= __thobj->wait_size);        \
-               (void *)__mptr(__thobj->wait_union);                    \
+               __mptr(__thobj->wait_union);                            \
        })
 
 #define threadobj_finish_wait()                do { } while (0)
diff --git a/lib/copperplate/heapobj-malloc.c b/lib/copperplate/heapobj-malloc.c
index b77cf09..2c66259 100644
--- a/lib/copperplate/heapobj-malloc.c
+++ b/lib/copperplate/heapobj-malloc.c
@@ -71,7 +71,7 @@ int __heapobj_init_private(struct heapobj *hobj, const char 
*name,
 
        ph->used = 0;
 
-       hobj->pool = ph;
+       hobj->private.pool = ph;
        hobj->size = size;
        if (name)
                snprintf(hobj->name, sizeof(hobj->name), "%s", name);
@@ -89,7 +89,7 @@ int heapobj_init_array_private(struct heapobj *hobj, const 
char *name,
 
 void pvheapobj_destroy(struct heapobj *hobj)
 {
-       struct pool_header *ph = hobj->pool;
+       struct pool_header *ph = hobj->private.pool;
 
        __RT(pthread_mutex_destroy(&ph->lock));
        __STD(free(ph));
@@ -97,7 +97,7 @@ void pvheapobj_destroy(struct heapobj *hobj)
 
 int pvheapobj_extend(struct heapobj *hobj, size_t size, void *mem)
 {
-       struct pool_header *ph = hobj->pool;
+       struct pool_header *ph = hobj->private.pool;
 
        write_lock_nocancel(&ph->lock);
        hobj->size += size;
@@ -108,7 +108,7 @@ int pvheapobj_extend(struct heapobj *hobj, size_t size, 
void *mem)
 
 void *pvheapobj_alloc(struct heapobj *hobj, size_t size)
 {
-       struct pool_header *ph = hobj->pool;
+       struct pool_header *ph = hobj->private.pool;
        struct block_header *bh;
        void *ptr;
 
@@ -143,7 +143,7 @@ fail:
 void pvheapobj_free(struct heapobj *hobj, void *ptr)
 {
        struct block_header *bh = ptr - sizeof(*bh);
-       struct pool_header *ph = hobj->pool;
+       struct pool_header *ph = hobj->private.pool;
 
        assert(hobj->size >= bh->size);
        __STD(free(bh));
@@ -154,7 +154,7 @@ void pvheapobj_free(struct heapobj *hobj, void *ptr)
 
 size_t pvheapobj_inquire(struct heapobj *hobj)
 {
-       struct pool_header *ph = hobj->pool;
+       struct pool_header *ph = hobj->private.pool;
 
        return ph->used;
 }
diff --git a/lib/copperplate/heapobj-pshared.c 
b/lib/copperplate/heapobj-pshared.c
index d7164c5..4235218 100644
--- a/lib/copperplate/heapobj-pshared.c
+++ b/lib/copperplate/heapobj-pshared.c
@@ -625,9 +625,10 @@ static int create_main_heap(pid_t *cnode_r)
         * bind to it.
         */
        snprintf(hobj->name, sizeof(hobj->name), "%s.heap", session);
-       snprintf(hobj->fsname, sizeof(hobj->fsname), "/xeno:%s", hobj->name);
+       snprintf(hobj->shared.fsname, sizeof(hobj->shared.fsname),
+                "/xeno:%s", hobj->name);
 
-       fd = shm_open(hobj->fsname, O_RDWR|O_CREAT, 0600);
+       fd = shm_open(hobj->shared.fsname, O_RDWR|O_CREAT, 0600);
        if (fd < 0)
                return __bt(-errno);
 
@@ -648,9 +649,10 @@ static int create_main_heap(pid_t *cnode_r)
 
        if (m_heap->cpid && kill(m_heap->cpid, 0) == 0) {
                if (m_heap->maplen == len) {
-                       hobj->pool = &m_heap->base;
+                       /* CAUTION: __moff() depends on __main_heap. */
                        __main_heap = m_heap;
                        __main_sysgroup = &m_heap->sysgroup;
+                       hobj->shared.pool = __moff(&m_heap->base);
                        goto done;
                }
                *cnode_r = m_heap->cpid;
@@ -674,7 +676,8 @@ init:
                goto unlink_fail;
 
        m_heap->maplen = len;
-       hobj->pool = &m_heap->base; /* Must be set prior to calling 
init_main_heap() */
+       /* CAUTION: init_main_heap() depends on hobj->shared.pool. */
+       hobj->shared.pool = __moff(&m_heap->base);
        ret = init_main_heap(m_heap, (caddr_t)m_heap + sizeof(*m_heap), size);
        if (ret) {
                errno = -ret;
@@ -696,7 +699,7 @@ unmap_fail:
        munmap(m_heap, len);
 unlink_fail:
        ret = __bt(-errno);
-       shm_unlink(hobj->fsname);
+       shm_unlink(hobj->shared.fsname);
        goto close_fail;
 errno_fail:
        ret = __bt(-errno);
@@ -717,9 +720,10 @@ static int bind_main_heap(const char *session)
        /* No error tracking, this is for internal users. */
 
        snprintf(hobj->name, sizeof(hobj->name), "%s.heap", session);
-       snprintf(hobj->fsname, sizeof(hobj->fsname), "/xeno:%s", hobj->name);
+       snprintf(hobj->shared.fsname, sizeof(hobj->shared.fsname),
+                "/xeno:%s", hobj->name);
 
-       fd = shm_open(hobj->fsname, O_RDWR, 0400);
+       fd = shm_open(hobj->shared.fsname, O_RDWR, 0400);
        if (fd < 0)
                return -errno;
 
@@ -749,7 +753,7 @@ static int bind_main_heap(const char *session)
                return -ENOENT;
        }
 
-       hobj->pool = &m_heap->base;
+       hobj->shared.pool = __moff(&m_heap->base);
        hobj->size = len - sizeof(*m_heap);
        __main_heap = m_heap;
        __main_catalog = &m_heap->catalog;
@@ -776,7 +780,7 @@ int pshared_check(void *__heap, void *__addr)
         * this one, so the address shall fall into the file-backed
         * memory range.
         */
-       if (heap == main_pool.pool) {
+       if (__moff(heap) == main_pool.shared.pool) {
                m_heap = container_of(heap, struct session_heap, base);
                return __addr >= (void *)m_heap &&
                        __addr < (void *)m_heap + m_heap->maplen;
@@ -838,7 +842,7 @@ int heapobj_init(struct heapobj *hobj, const char *name, 
size_t size)
        else
                snprintf(hobj->name, sizeof(hobj->name), "%s.%p", session, 
hobj);
 
-       hobj->pool = heap;
+       hobj->shared.pool = __moff(heap);
        hobj->size = size;
        init_heap(heap, hobj->name, (caddr_t)heap + sizeof(*heap), size);
        sysgroup_add(heap, &heap->memspec);
@@ -855,7 +859,7 @@ int heapobj_init_array(struct heapobj *hobj, const char 
*name,
 
 void heapobj_destroy(struct heapobj *hobj)
 {
-       struct shared_heap *heap = hobj->pool;
+       struct shared_heap *heap = __mptr(hobj->shared.pool);
        int cpid;
 
        if (hobj != &main_pool) {
@@ -874,12 +878,12 @@ void heapobj_destroy(struct heapobj *hobj)
        __RT(pthread_mutex_destroy(&heap->lock));
        __RT(pthread_mutex_destroy(&main_heap.sysgroup.lock));
        munmap(&main_heap, main_heap.maplen);
-       shm_unlink(hobj->fsname);
+       shm_unlink(hobj->shared.fsname);
 }
 
 int heapobj_extend(struct heapobj *hobj, size_t size, void *unused)
 {
-       struct shared_heap *heap = hobj->pool;
+       struct shared_heap *heap = __mptr(hobj->shared.pool);
        struct shared_extent *extent;
        int state;
 
@@ -906,22 +910,22 @@ int heapobj_extend(struct heapobj *hobj, size_t size, 
void *unused)
 
 void *heapobj_alloc(struct heapobj *hobj, size_t size)
 {
-       return alloc_block(hobj->pool, size);
+       return alloc_block(__mptr(hobj->shared.pool), size);
 }
 
 void heapobj_free(struct heapobj *hobj, void *ptr)
 {
-       free_block(hobj->pool, ptr);
+       free_block(__mptr(hobj->shared.pool), ptr);
 }
 
 size_t heapobj_validate(struct heapobj *hobj, void *ptr)
 {
-       return __bt(check_block(hobj->pool, ptr));
+       return __bt(check_block(__mptr(hobj->shared.pool), ptr));
 }
 
 size_t heapobj_inquire(struct heapobj *hobj)
 {
-       struct shared_heap *heap = hobj->pool;
+       struct shared_heap *heap = __mptr(hobj->shared.pool);
        return heap->ubytes;
 }
 
diff --git a/lib/copperplate/heapobj-tlsf.c b/lib/copperplate/heapobj-tlsf.c
index e573380..7b1ceef 100644
--- a/lib/copperplate/heapobj-tlsf.c
+++ b/lib/copperplate/heapobj-tlsf.c
@@ -53,7 +53,7 @@ int __heapobj_init_private(struct heapobj *hobj, const char 
*name,
        else
                snprintf(hobj->name, sizeof(hobj->name), "%p", hobj);
 
-       hobj->pool = mem;
+       hobj->private.pool = mem;
        /* Make sure to wipe out tlsf's signature. */
        memset(mem, 0, size < 32 ? size : 32);
        hobj->size = init_memory_pool(size, mem);


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

Reply via email to