[Xenomai-git] Philippe Gerum : copperplate/heapobj-pshared: fix and sanitize extent management

2015-08-13 Thread git repository hosting
Module: xenomai-3
Branch: next
Commit: 65f16f4a3f0d4de3be221bea6b4545b2b4d74321
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=65f16f4a3f0d4de3be221bea6b4545b2b4d74321

Author: Philippe Gerum r...@xenomai.org
Date:   Wed Aug 12 18:27:22 2015 +0200

copperplate/heapobj-pshared: fix and sanitize extent management

---

 lib/copperplate/heapobj-pshared.c |  278 +
 lib/copperplate/internal.h|3 -
 2 files changed, 159 insertions(+), 122 deletions(-)

diff --git a/lib/copperplate/heapobj-pshared.c 
b/lib/copperplate/heapobj-pshared.c
index 5fe899a..0df08c3 100644
--- a/lib/copperplate/heapobj-pshared.c
+++ b/lib/copperplate/heapobj-pshared.c
@@ -57,17 +57,17 @@ enum {
page_list =2
 };
 
-struct page_map {
+struct page_entry {
unsigned int type : 8;/* free, cont, list or log2 */
unsigned int bcount : 24; /* Number of active blocks. */
 };
 
 struct shared_extent {
struct holder link;
-   memoff_t membase;   /* Base address of the page array */
-   memoff_t memlim;/* Memory limit of page array */
-   memoff_t freelist;  /* Head of the free page list */
-   struct page_map pagemap[1]; /* Start of page map */
+   memoff_t membase;   /* Base offset of page array */
+   memoff_t memlim;/* Offset limit of page array */
+   memoff_t freelist;  /* Head of free page list */
+   struct page_entry pagemap[1]; /* Start of page map */
 };
 
 /*
@@ -75,7 +75,7 @@ struct shared_extent {
  * additional session-wide information.
  */
 struct session_heap {
-   struct shared_heap base;
+   struct shared_heap heap;
int cpid;
memoff_t maplen;
struct hash_table catalog;
@@ -91,6 +91,12 @@ struct session_heap {
  */
 void *__main_heap;
 #define main_heap  (*(struct session_heap *)__main_heap)
+/*
+ *  Base address for offset-based addressing, which is the start of
+ *  the session heap since all memory objects are allocated from it,
+ *  including other (sub-)heaps.
+ */
+#define main_base  __main_heap
 
 /* A table of shared clusters for the session. */
 struct hash_table *__main_catalog;
@@ -100,10 +106,10 @@ struct sysgroup *__main_sysgroup;
 
 static struct heapobj main_pool;
 
-#define __shoff(h, p)  ((caddr_t)(p) - (caddr_t)(h))
-#define __shoff_check(h, p)((p) ? __shoff(h, p) : 0)
-#define __shref(h, o)  ((void *)((caddr_t)(h) + (o)))
-#define __shref_check(h, o)((o) ? __shref(h, o) : NULL)
+#define __shoff(b, p)  ((caddr_t)(p) - (caddr_t)(b))
+#define __shoff_check(b, p)((p) ? __shoff(b, p) : 0)
+#define __shref(b, o)  ((void *)((caddr_t)(b) + (o)))
+#define __shref_check(b, o)((o) ? __shref(b, o) : NULL)
 
 static inline size_t __align_to(size_t size, size_t al)
 {
@@ -111,33 +117,39 @@ static inline size_t __align_to(size_t size, size_t al)
return ((size+al-1)(~(al-1)));
 }
 
-static inline size_t internal_overhead(size_t hsize)
+static inline size_t get_pagemap_size(size_t h)
 {
-   /* o = (h - o) * m / p + e
-  o * p = (h - o) * m + e * p
-  o * (p + m) = h * m + e * p
-  o = (h * m + e *p) / (p + m)
-   */
-   return __align_to((sizeof(struct shared_extent) * HOBJ_PAGE_SIZE
-  + sizeof(struct page_map) * hsize)
- / (HOBJ_PAGE_SIZE + sizeof(struct page_map)), 
HOBJ_PAGE_SIZE);
+   /*
+* Return the size of the meta data required to map 'h' bytes
+* of user memory in pages of HOBJ_PAGE_SIZE bytes. The meta
+* data includes the length of the extent descriptor, plus the
+* length of the page mapping array. 'h' must be a multiple of
+* HOBJ_PAGE_SIZE on entry.
+*/
+   assert((h  ~HOBJ_PAGE_MASK) == 0);
+   return __align_to((h  HOBJ_PAGE_SHIFT) * sizeof(struct page_entry)
+ + sizeof(struct shared_extent), HOBJ_PAGE_SIZE);
 }
 
-static void init_extent(struct shared_heap *heap, struct shared_extent *extent)
+static void init_extent(void *base, struct shared_extent *extent)
 {
caddr_t freepage;
int n, lastpgnum;
 
-   __holder_init_nocheck(heap, extent-link);
+   __holder_init_nocheck(base, extent-link);
 
-   /* The initial extent starts right after the header. */
-   extent-membase = __shoff(heap, extent) + heap-hdrsize;
-   lastpgnum = heap-npages - 1;
+   lastpgnum = ((extent-memlim - extent-membase)  HOBJ_PAGE_SHIFT) - 1;
+   /*
+* An extent must contain at least two addressable pages to
+* cope with allocation sizes between PAGESIZE and 2 *
+* PAGESIZE.
+*/
+   assert(lastpgnum = 1);
 
/* Mark each page as free in the page map. */
-   for (n = 0, freepage = __shref(heap, extent-membase);
+   for (n = 0, freepage = __shref(base, extent-membase);
 n  lastpgnum; n++, freepage += HOBJ_PAGE_SIZE) {
-  

[Xenomai-git] Philippe Gerum : vxworks/memPartLib: do not account for failed alloc

2015-08-13 Thread git repository hosting
Module: xenomai-3
Branch: next
Commit: f928e4d39bf62c8bae1ceeda7d403d76e3a37ed8
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=f928e4d39bf62c8bae1ceeda7d403d76e3a37ed8

Author: Philippe Gerum r...@xenomai.org
Date:   Wed Aug 12 10:31:14 2015 +0200

vxworks/memPartLib: do not account for failed alloc

---

 lib/vxworks/memPartLib.c |4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/vxworks/memPartLib.c b/lib/vxworks/memPartLib.c
index 932fc90..09adb13 100644
--- a/lib/vxworks/memPartLib.c
+++ b/lib/vxworks/memPartLib.c
@@ -156,6 +156,8 @@ void *memPartAlloc(PART_ID partId, unsigned int nBytes)
__RT(pthread_mutex_lock(mp-lock));
 
p = heapobj_alloc(mp-hobj, nBytes);
+   if (p == NULL)
+   goto out;
 
mp-stats.numBytesAlloc += nBytes;
mp-stats.numBlocksAlloc++;
@@ -163,7 +165,7 @@ void *memPartAlloc(PART_ID partId, unsigned int nBytes)
mp-stats.numBlocksFree--;
if (mp-stats.numBytesAlloc  mp-stats.maxBytesAlloc)
mp-stats.maxBytesAlloc = mp-stats.numBytesAlloc;
-
+out:
__RT(pthread_mutex_unlock(mp-lock));
 
return p;


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


[Xenomai-git] Philippe Gerum : vxworks/memPartLib: fix invalid status from addToPool

2015-08-13 Thread git repository hosting
Module: xenomai-3
Branch: next
Commit: f1749e3d72b9a27e92cf066c356c0922a03c
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=f1749e3d72b9a27e92cf066c356c0922a03c

Author: Philippe Gerum r...@xenomai.org
Date:   Wed Aug 12 10:22:58 2015 +0200

vxworks/memPartLib: fix invalid status from addToPool

---

 lib/vxworks/memPartLib.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/vxworks/memPartLib.c b/lib/vxworks/memPartLib.c
index 7bb5c6a..932fc90 100644
--- a/lib/vxworks/memPartLib.c
+++ b/lib/vxworks/memPartLib.c
@@ -83,7 +83,7 @@ STATUS memPartAddToPool(PART_ID partId,
 {
struct wind_mempart *mp;
struct service svc;
-   STATUS ret;
+   STATUS ret = OK;
 
if (poolSize == 0) {
errno = S_memLib_INVALID_NBYTES;


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


[Xenomai-git] Philippe Gerum : copperplate/init: disambiguate --mem-pool-size argument

2015-08-13 Thread git repository hosting
Module: xenomai-3
Branch: next
Commit: b151d3ba663ec5abd008ff2f3f4ce6b3ce9d02b2
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=b151d3ba663ec5abd008ff2f3f4ce6b3ce9d02b2

Author: Philippe Gerum r...@xenomai.org
Date:   Thu Aug 13 17:07:42 2015 +0200

copperplate/init: disambiguate --mem-pool-size argument

The value passed to --mem-pool-size used to represent kilobytes, which
is at odds with the byte unit required by the mem_pool_size tunable.

--mem-pool-size now accepts a unit K|M|G suffix to values, defaulting
to bytes. For backward compatibility, any non-suffixed value below
65536 is interpreted as a count of kilobytes, not bytes,
e.g. --mem-pool-size=8192 will create a 8M heap for the
application. This conversion is noisy, to call for a parameter update
asap, unless --no-sanity is in effect.

If you need a tiny heap size below 64k, pass a kilobyte value with a 'K'
suffix.

---

 lib/copperplate/init.c |   20 ++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/lib/copperplate/init.c b/lib/copperplate/init.c
index c84d8a2..d24673d 100644
--- a/lib/copperplate/init.c
+++ b/lib/copperplate/init.c
@@ -21,6 +21,7 @@
 #include stdlib.h
 #include unistd.h
 #include string.h
+#include ctype.h
 #include pwd.h
 #include errno.h
 #include getopt.h
@@ -193,9 +194,24 @@ static int copperplate_init(void)
 
 static int copperplate_parse_option(int optnum, const char *optarg)
 {
+   size_t memsz;
+
switch (optnum) {
case mempool_opt:
-   __copperplate_setup_data.mem_pool = atoi(optarg) * 1024;
+   memsz = get_mem_size(optarg);
+   if (memsz == 0)
+   return -EINVAL;
+   /*
+* Emulate former sloppy syntax: values below 64k are
+* likely to represent kilobytes, not bytes.
+*/
+   if (isdigit(optarg[strlen(optarg)-1]) 
+   memsz  64 * 1024) {
+   memsz *= 1024;
+   if (__base_setup_data.no_sanity == 0)
+   warning(--mem-pool-size=size[K|M|G], using 
%Zu bytes, memsz);
+   }
+   __copperplate_setup_data.mem_pool = memsz;
break;
case session_opt:
__copperplate_setup_data.session_label = strdup(optarg);
@@ -216,7 +232,7 @@ static int copperplate_parse_option(int optnum, const char 
*optarg)
 
 static void copperplate_help(void)
 {
-   fprintf(stderr, --mem-pool-size=sizeKsize of the main heap 
(kbytes)\n);
+   fprintf(stderr, --mem-pool-size=size[K|M|G]  size of the main 
heap\n);
 fprintf(stderr, --no-registry suppress object 
registration\n);
 fprintf(stderr, --shared-registry enable public access to 
registry\n);
 fprintf(stderr, --registry-root=pathroot path of 
registry\n);


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


[Xenomai-git] Philippe Gerum : boilerplate/ancillaries: add routine to parse mem size

2015-08-13 Thread git repository hosting
Module: xenomai-3
Branch: next
Commit: 0da6b0d20fcdd0f06b8b3be0280a40b20bb7
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=0da6b0d20fcdd0f06b8b3be0280a40b20bb7

Author: Philippe Gerum r...@xenomai.org
Date:   Thu Aug 13 17:07:37 2015 +0200

boilerplate/ancillaries: add routine to parse mem size

---

 include/boilerplate/ancillaries.h |4 +++-
 lib/boilerplate/ancillaries.c |   31 +++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/include/boilerplate/ancillaries.h 
b/include/boilerplate/ancillaries.h
index 1ba320b..6b87f0c 100644
--- a/include/boilerplate/ancillaries.h
+++ b/include/boilerplate/ancillaries.h
@@ -111,7 +111,9 @@ int get_static_cpu_count(void);
 pid_t get_thread_pid(void);
 
 char *lookup_command(const char *cmd);
-  
+
+size_t get_mem_size(const char *arg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/boilerplate/ancillaries.c b/lib/boilerplate/ancillaries.c
index 6c8be35..848adc7 100644
--- a/lib/boilerplate/ancillaries.c
+++ b/lib/boilerplate/ancillaries.c
@@ -23,8 +23,10 @@
 #include stdarg.h
 #include pthread.h
 #include unistd.h
+#include ctype.h
 #include string.h
 #include errno.h
+#include limits.h
 #include malloc.h
 #include boilerplate/atomic.h
 #include boilerplate/lock.h
@@ -319,6 +321,35 @@ char *lookup_command(const char *cmd)
return NULL;
 }
 
+size_t get_mem_size(const char *arg)
+{
+   size_t size;
+   char *p;
+
+   size = strtol(arg, p, 0);
+   if (size == LONG_MIN || size == LONG_MAX)
+   return 0;
+
+   if (*p == '\0')
+   return size;
+
+   switch (tolower(*p)) {
+   case 'k':
+   size *= 1024;
+   break;
+   case 'm':
+   size *= (1024 * 1024);
+   break;
+   case 'g':
+   size *= (1024 * 1024 * 1024);
+   break;
+   default:
+   size = 0;
+   }
+
+   return size;
+}
+
 const char *config_strings[] = {
 #include config-dump.h
NULL,


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