Subversion Repositories HelenOS

Rev

Rev 1868 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1868 Rev 2015
Line 35... Line 35...
35
#include <as.h>
35
#include <as.h>
36
#include <libc.h>
36
#include <libc.h>
37
#include <unistd.h>
37
#include <unistd.h>
38
#include <align.h>
38
#include <align.h>
39
#include <types.h>
39
#include <types.h>
-
 
40
#include <bitops.h>
40
 
41
 
41
/**
42
/**
42
 * Either 4*256M on 32-bit architecures or 16*256M on 64-bit architectures.
43
 * Either 4*256M on 32-bit architecures or 16*256M on 64-bit architectures.
43
 */
44
 */
44
#define MAX_HEAP_SIZE   (sizeof(uintptr_t)<<28)
45
#define MAX_HEAP_SIZE   (sizeof(uintptr_t)<<28)
Line 51... Line 52...
51
 *
52
 *
52
 * @return address on success, (void *) -1 otherwise.
53
 * @return address on success, (void *) -1 otherwise.
53
 */
54
 */
54
void *as_area_create(void *address, size_t size, int flags)
55
void *as_area_create(void *address, size_t size, int flags)
55
{
56
{
56
    return (void *) __SYSCALL3(SYS_AS_AREA_CREATE, (sysarg_t ) address, (sysarg_t) size, (sysarg_t) flags);
57
    return (void *) __SYSCALL3(SYS_AS_AREA_CREATE, (sysarg_t ) address,
-
 
58
        (sysarg_t) size, (sysarg_t) flags);
57
}
59
}
58
 
60
 
59
/** Resize address space area.
61
/** Resize address space area.
60
 *
62
 *
61
 * @param address Virtual address pointing into already existing address space area.
63
 * @param address Virtual address pointing into already existing address space
-
 
64
 *  area.
62
 * @param size New requested size of the area.
65
 * @param size New requested size of the area.
63
 * @param flags Currently unused.
66
 * @param flags Currently unused.
64
 *
67
 *
65
 * @return Zero on success or a code from @ref errno.h on failure.
68
 * @return Zero on success or a code from @ref errno.h on failure.
66
 */
69
 */
67
int as_area_resize(void *address, size_t size, int flags)
70
int as_area_resize(void *address, size_t size, int flags)
68
{
71
{
69
    return __SYSCALL3(SYS_AS_AREA_RESIZE, (sysarg_t ) address, (sysarg_t) size, (sysarg_t) flags);
72
    return __SYSCALL3(SYS_AS_AREA_RESIZE, (sysarg_t ) address, (sysarg_t)
-
 
73
        size, (sysarg_t) flags);
70
}
74
}
71
 
75
 
72
/** Destroy address space area.
76
/** Destroy address space area.
73
 *
77
 *
74
 * @param address Virtual address pointing into the address space area being destroyed.
78
 * @param address Virtual address pointing into the address space area being
-
 
79
 *  destroyed.
75
 *
80
 *
76
 * @return Zero on success or a code from @ref errno.h on failure.
81
 * @return Zero on success or a code from @ref errno.h on failure.
77
 */
82
 */
78
int as_area_destroy(void *address)
83
int as_area_destroy(void *address)
79
{
84
{
Line 131... Line 136...
131
void *set_maxheapsize(size_t mhs)
136
void *set_maxheapsize(size_t mhs)
132
{
137
{
133
    maxheapsize = mhs;
138
    maxheapsize = mhs;
134
    /* Return pointer to area not managed by sbrk */
139
    /* Return pointer to area not managed by sbrk */
135
    return ((void *) &_heap + maxheapsize);
140
    return ((void *) &_heap + maxheapsize);
136
 
-
 
137
}
141
}
138
 
142
 
139
/** Return pointer to some unmapped area, where fits new as_area
143
/** Return pointer to some unmapped area, where fits new as_area
140
 *
144
 *
-
 
145
 * @param sz Requested size of the allocation.
-
 
146
 * @param color Requested virtual color of the allocation.
-
 
147
 *
-
 
148
 * @return Pointer to the beginning
-
 
149
 *
141
 * TODO: make some first_fit/... algorithm, we are now just incrementing
150
 * TODO: make some first_fit/... algorithm, we are now just incrementing
142
 *       the pointer to last area
151
 *       the pointer to last area
143
 */
152
 */
-
 
153
#include <stdio.h>
144
void * as_get_mappable_page(size_t sz)
154
void *as_get_mappable_page(size_t sz, int color)
145
{
155
{
146
    void *res;
156
    void *res;
-
 
157
    uint64_t asz;
-
 
158
    int i;
-
 
159
   
-
 
160
    if (!sz)
-
 
161
        return NULL;   
-
 
162
 
-
 
163
    asz = 1 << (fnzb64(sz - 1) + 1);
147
 
164
 
148
    /* Set heapsize to some meaningful value */
165
    /* Set heapsize to some meaningful value */
149
    if (maxheapsize == -1)
166
    if (maxheapsize == -1)
150
        set_maxheapsize(MAX_HEAP_SIZE);
167
        set_maxheapsize(MAX_HEAP_SIZE);
151
   
168
   
-
 
169
    /*
-
 
170
     * Make sure we allocate from naturally aligned address and a page of
-
 
171
     * appropriate color.
-
 
172
     */
-
 
173
    i = 0;
-
 
174
    do {
152
    if (!last_allocated)
175
        if (!last_allocated) {
153
        last_allocated = (void *) ALIGN_UP((void *) &_heap + maxheapsize, PAGE_SIZE);
176
            last_allocated = (void *) ALIGN_UP((void *) &_heap +
-
 
177
                maxheapsize, asz);
-
 
178
        } else {
-
 
179
            last_allocated = (void *) ALIGN_UP(((uintptr_t)
-
 
180
                last_allocated) + (int) (i > 0), asz);
154
   
181
        }
-
 
182
    } while ((asz < (1 << (PAGE_COLOR_BITS + PAGE_WIDTH))) &&
-
 
183
        (PAGE_COLOR((uintptr_t) last_allocated) != color) &&
155
    sz = ALIGN_UP(sz, PAGE_SIZE);
184
        (++i < (1 << PAGE_COLOR_BITS)));
-
 
185
 
156
    res = last_allocated;
186
    res = last_allocated;
157
    last_allocated += sz;
187
    last_allocated += ALIGN_UP(sz, PAGE_SIZE);
158
 
188
 
159
    return res;
189
    return res;
160
}
190
}
161
 
191
 
162
/** @}
192
/** @}