|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +// These memory functions are needed when the garbage collector is disabled. |
| 30 | +// A full implementation should be provided, or the garbage collector enabled. |
| 31 | +// The functions here are very simple. |
| 32 | + |
| 33 | +extern char _heap_start; |
| 34 | + |
| 35 | +void *malloc(size_t n) { |
| 36 | + static char *cur_heap = NULL; |
| 37 | + if (cur_heap == NULL) { |
| 38 | + cur_heap = &_heap_start; |
| 39 | + } |
| 40 | + void *ptr = cur_heap; |
| 41 | + cur_heap += (n + 7) & ~7; |
| 42 | + return ptr; |
| 43 | +} |
| 44 | + |
| 45 | +void *realloc(void *ptr, size_t size) { |
| 46 | + void *ptr2 = malloc(size); |
| 47 | + if (ptr && size) { |
| 48 | + memcpy(ptr2, ptr, size); // size may be greater than ptr's region, do copy anyway |
| 49 | + } |
| 50 | + return ptr2; |
| 51 | +} |
| 52 | + |
| 53 | +void free(void *p) { |
| 54 | +} |
| 55 | + |
| 56 | +// These standard string functions are needed by the runtime, and can be |
| 57 | +// provided either by the system or lib/libc/string0.c. The implementations |
| 58 | +// here are very simple. |
| 59 | + |
| 60 | +int memcmp(const void *s1, const void *s2, size_t n) { |
| 61 | + const unsigned char *ss1 = s1, *ss2 = s2; |
| 62 | + while (n--) { |
| 63 | + int c = *ss1++ - *ss2++; |
| 64 | + if (c) { |
| 65 | + return c; |
| 66 | + } |
| 67 | + } |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +void *memcpy(void *dest, const void *src, size_t n) { |
| 72 | + return memmove(dest, src, n); |
| 73 | +} |
| 74 | + |
| 75 | +void *memmove(void *dest, const void *src, size_t n) { |
| 76 | + unsigned char *d = dest; |
| 77 | + const unsigned char *s = src; |
| 78 | + if (s < d && d < s + n) { |
| 79 | + // Need to copy backwards. |
| 80 | + d += n - 1; |
| 81 | + s += n - 1; |
| 82 | + while (n--) { |
| 83 | + *d-- = *s--; |
| 84 | + } |
| 85 | + } else { |
| 86 | + // Can copy forwards. |
| 87 | + while (n--) { |
| 88 | + *d++ = *s++; |
| 89 | + } |
| 90 | + } |
| 91 | + return dest; |
| 92 | +} |
| 93 | + |
| 94 | +void *memset(void *s, int c, size_t n) { |
| 95 | + unsigned char *ss = s; |
| 96 | + while (n--) { |
| 97 | + *ss++ = c; |
| 98 | + } |
| 99 | + return s; |
| 100 | +} |
| 101 | + |
| 102 | +char *strchr(const char *s, int c) { |
| 103 | + while (*s) { |
| 104 | + if (*s == c) { |
| 105 | + return (char *)s; |
| 106 | + } |
| 107 | + ++s; |
| 108 | + } |
| 109 | + return NULL; |
| 110 | +} |
| 111 | + |
| 112 | +int strcmp(const char *s1, const char *s2) { |
| 113 | + while (*s1 && *s2) { |
| 114 | + int c = *s1++ - *s2++; |
| 115 | + if (c) { |
| 116 | + return c; |
| 117 | + } |
| 118 | + } |
| 119 | + return *s1 - *s2; |
| 120 | +} |
| 121 | + |
| 122 | +size_t strlen(const char *s) { |
| 123 | + const char *ss = s; |
| 124 | + while (*ss) { |
| 125 | + ++ss; |
| 126 | + } |
| 127 | + return ss - s; |
| 128 | +} |
0 commit comments