hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2008 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "boilerplate/tlsf/tlsf.h"
#include "copperplate/heapobj.h"
#include "copperplate/debug.h"
#include "copperplate/threadobj.h"
#include "xenomai/init.h"
#include "internal.h"
 
#if LONG_BIT == 32
#define TLSF_BLOCK_ALIGN  (8 * 2)
#else
#define TLSF_BLOCK_ALIGN  (16 * 2)
#endif
 
static int tlsf_pool_overhead;
 
int __heapobj_init_private(struct heapobj *hobj, const char *name,
              size_t size, void *mem)
{
   if (mem == NULL) {
       /*
        * When the memory area is unspecified, obtain it from
        * the main pool, accounting for the TLSF overhead.
        */
       size += tlsf_pool_overhead;
       mem = tlsf_malloc(size);
       if (mem == NULL)
           return __bt(-ENOMEM);
   }
 
   if (name)
       snprintf(hobj->name, sizeof(hobj->name), "%s", name);
   else
       snprintf(hobj->name, sizeof(hobj->name), "%p", hobj);
 
   hobj->pool = mem;
   /* Make sure to wipe out tlsf's signature. */
   memset(mem, 0, size < 32 ? size : 32);
   hobj->size = init_memory_pool(size, mem);
   if (hobj->size == (size_t)-1)
       return __bt(-EINVAL);
 
   return 0;
}
 
int heapobj_init_array_private(struct heapobj *hobj, const char *name,
                  size_t size, int elems)
{
   size_t poolsz;
 
   if (size == 0 || elems <= 0)
       return __bt(-EINVAL);
 
   poolsz = (size + TLSF_BLOCK_ALIGN - 1) & ~(TLSF_BLOCK_ALIGN - 1);
   poolsz *= elems;
 
   return __bt(__heapobj_init_private(hobj, name, poolsz, NULL));
}
 
int heapobj_pkg_init_private(void)
{
   size_t alloc_size, available_size;
   void *mem;
 
#ifdef CONFIG_XENO_PSHARED
   alloc_size = MIN_TLSF_HEAPSZ;
#else
   alloc_size = __copperplate_setup_data.mem_pool;
   if (alloc_size < MIN_TLSF_HEAPSZ)
       alloc_size = MIN_TLSF_HEAPSZ;
#endif
   /*
    * We want to know how many bytes from a memory pool TLSF will
    * use for its own internal use. We get the probe memory from
    * tlsf_malloc(), so that the main pool will be set up in the
    * same move.
    *
    * We include 1k of additional memory to cope with the
    * per-block overhead for an undefined number of individual
    * allocation requests. Ugly.
    *
    * CAUTION: in pshared mode, private heaps are subsidiary
    * storage pools, so no need to pre-commit as much memory as
    * we will be preallocating for the main shared pool,
    * especially with memory locking in effect. In that case,
    * creating a temporary single-page pool is enough to figure
    * out the allocation overhead.
    */
   mem = tlsf_malloc(alloc_size);
   available_size = init_memory_pool(alloc_size, mem);
   if (available_size == (size_t)-1)
       panic("cannot initialize TLSF memory manager");
 
   destroy_memory_pool(mem);
   tlsf_pool_overhead = alloc_size - available_size;
   tlsf_pool_overhead = (tlsf_pool_overhead + 1024) & ~15;
   tlsf_free(mem);
 
   return 0;
}