All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai-core] [PATCH] fix xnheap_alloc rounding
@ 2006-07-11 14:02 Jan Kiszka
  2006-07-12 12:26 ` Gilles Chanteperdrix
  2006-07-15  1:48 ` Philippe Gerum
  0 siblings, 2 replies; 6+ messages in thread
From: Jan Kiszka @ 2006-07-11 14:02 UTC (permalink / raw)
  To: xenomai-core


[-- Attachment #1.1: Type: text/plain, Size: 1024 bytes --]

Hi,

playing a stupid rt_heap user (actually I didn't just play this...), I
stumbled over this undocumented oddity:

rt_heap_create(&heap, name, 10000, H_PRIO|H_MAPPABLE);
rt_heap_alloc(&heap, 10000, TM_NONBLOCK, &ptr);

Creation is successful, allocation fails. The reason: while during
creation the net heap size is rounded down to page boundaries, the
allocation of memory > PAGE_SIZE is rounded up. One could add H_SINGLE
to the flags, but this may even result in allocating less memory than
the user expected, causing severe problems later.

How to resolve this best? I thought about rounding twice in
rt_head_create (one time the net size, the second time including the
overhead), but this encodes characteristics of the underlying heap
allocator into the skin (I have a generic heap allocator framework in
mind for 2.3). So I decided to do this rounding in xnheap_overhead()
instead, see attached patch. Hope I didn't skewed up any calculation. At
least the scenario above now works fine.

Jan

[-- Attachment #1.2: xnheap_overhead-fix-rounding.patch --]
[-- Type: text/plain, Size: 927 bytes --]

Index: include/nucleus/heap.h
===================================================================
--- include/nucleus/heap.h	(revision 1322)
+++ include/nucleus/heap.h	(working copy)
@@ -109,8 +109,14 @@ extern xnheap_t kheap;
 #define xnheap_used_mem(heap)        ((heap)->ubytes)
 #define xnheap_max_contiguous(heap)  ((heap)->maxcont)
 #define xnheap_overhead(hsize,psize) \
-((sizeof(xnextent_t) + (((hsize) - sizeof(xnextent_t)) / (psize)) + \
- XNHEAP_MINALIGNSZ - 1) & ~(XNHEAP_MINALIGNSZ - 1))
+({ \
+    u_long rounded_hsize = (hsize + psize - 1) & ~(psize - 1); \
+    u_long overhead = ((sizeof(xnextent_t) + \
+	(((rounded_hsize) - sizeof(xnextent_t)) / (psize)) + \
+	XNHEAP_MINALIGNSZ - 1) & ~(XNHEAP_MINALIGNSZ - 1)); \
+    overhead += rounded_hsize - hsize; \
+    overhead; \
+})
 
 #define xnmalloc(size)     xnheap_alloc(&kheap,size)
 #define xnfree(ptr)        xnheap_free(&kheap,ptr)

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 250 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-07-15  8:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-07-11 14:02 [Xenomai-core] [PATCH] fix xnheap_alloc rounding Jan Kiszka
2006-07-12 12:26 ` Gilles Chanteperdrix
2006-07-12 12:38   ` Jan Kiszka
2006-07-15  1:48 ` Philippe Gerum
2006-07-15  8:07   ` Jan Kiszka
2006-07-15  8:19     ` Philippe Gerum

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.