RTEMS
memoryallocate.c
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2018, 2019 embedded brains GmbH
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <rtems/score/memory.h>
33 
35  const Memory_Information *information,
36  uintptr_t size,
37  uintptr_t alignment
38 )
39 {
40  Memory_Area *area;
41  const Memory_Area *end;
42  uintptr_t alignment_mask;
43 
44  area = &information->areas[ 0 ];
45  end = &information->areas[ information->count ];
46  alignment_mask = alignment - 1;
47 
48  while ( area != end ) {
49  uintptr_t alloc_begin;
50  uintptr_t alloc_end;
51 
52  alloc_begin = (uintptr_t) area->free;
53  alloc_begin += alignment_mask;
54  alloc_begin &= ~alignment_mask;
55  alloc_end = alloc_begin + size;
56 
57  if ( alloc_begin <= alloc_end && alloc_end <= (uintptr_t) area->end ) {
58  area->free = (void *) alloc_end;
59 
60  return (void *) alloc_begin;
61  }
62 
63  ++area;
64  }
65 
66  return NULL;
67 }
Memory Handler API.
void * free
A pointer to the begin of the free area of the memory area.
Definition: memory.h:69
Memory_Area * areas
The memory area table.
Definition: memory.h:89
size_t count
The count of memory areas.
Definition: memory.h:84
The memory information.
Definition: memory.h:80
The memory area description.
Definition: memory.h:60
const void * end
A pointer to the end of the memory area.
Definition: memory.h:74
void * _Memory_Allocate(const Memory_Information *information, uintptr_t size, uintptr_t alignment)
Allocate a memory area from the memory information.