• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * TODO: temporary, replace this with pass-throughs between errno style
19  * numbers and LK errors.
20  */
21 
22 #define LACKS_TIME_H
23 #define LACKS_SYS_MMAN_H
24 #define LACKS_FCNTL_H
25 #define LACKS_UNISTD_H
26 #define LACKS_SYS_PARAM_H
27 #define LACKS_ERRNO_H
28 
29 #define MALLOC_FAILURE_ACTION \
30     do {                      \
31     } while (0)
32 #define ABORT abort()
33 #define HAVE_MMAP 0
34 #define MORECORE sbrk
35 
36 #include <sys/auxv.h>
37 #define malloc_getpagesize getauxval(AT_PAGESZ)
38 
39 #define ENOMEM ERR_NO_MEMORY
40 #define EINVAL ERR_INVALID_ARGS
41 
42 #include <errno.h>
43 #include <lk/macros.h>
44 #include <stddef.h>
45 #include <stdlib.h>
46 #include <trusty_syscalls.h>
47 #include <uapi/err.h>
48 #include <unistd.h>
49 
50 static char* __libc_brk;
51 
52 #define SBRK_ALIGN 32
sbrk(ptrdiff_t increment)53 void* sbrk(ptrdiff_t increment) {
54     char* new_brk;
55     char* start;
56     char* end;
57 
58     if (!__libc_brk)
59         __libc_brk = (char*)_trusty_brk(0);
60 
61     start = (char*)round_up((uintptr_t)__libc_brk, SBRK_ALIGN);
62     end = (char*)round_up((uintptr_t)(start + increment), SBRK_ALIGN);
63 
64     new_brk = (char*)_trusty_brk(end);
65     if (new_brk < end) {
66         errno = ENOMEM;
67         return (void*)-1;
68     }
69 
70     __libc_brk = new_brk;
71     return start;
72 }
73 
74 #include "dlmalloc.c"
75