| 1 | /* |
| 2 | * (C) Copyright 2008 Openmoko, Inc. |
| 3 | * Author: Andy Green <andy@openmoko.org> |
| 4 | * |
| 5 | * Little utils for print and strings |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <qi.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | extern void (*putc_func)(char); |
| 27 | |
| 28 | /* |
| 29 | * malloc pool needs to be in phase 2 bss section, we have phase 1 bss in |
| 30 | * steppingstone to allow full memory range testing in C |
| 31 | */ |
| 32 | u8 malloc_pool[MALLOC_POOL_EXTENT]; |
| 33 | void * malloc_pointer = &malloc_pool[0]; |
| 34 | |
| 35 | |
| 36 | /* improbably simple malloc and free for small and non-intense allocation |
| 37 | * just moves the allocation ptr forward each time and ignores free |
| 38 | */ |
| 39 | |
| 40 | void *malloc(size_t size) |
| 41 | { |
| 42 | void *p = malloc_pointer; |
| 43 | |
| 44 | malloc_pointer += (size & ~3) + 4; |
| 45 | |
| 46 | if (((u8 *)malloc_pointer - &malloc_pool[0]) > sizeof(malloc_pool)) { |
| 47 | puts("Ran out of malloc pool\n"); |
| 48 | while (1) |
| 49 | ; |
| 50 | } |
| 51 | |
| 52 | return p; |
| 53 | } |
| 54 | |
| 55 | void free(void *ptr) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | char *strncpy(char *dest, const char *src, size_t n) |
| 60 | { |
| 61 | char * dest_orig = dest; |
| 62 | |
| 63 | while (*src && n--) |
| 64 | *dest++ = *src++; |
| 65 | |
| 66 | if (n) |
| 67 | *dest = '\0'; |
| 68 | |
| 69 | return dest_orig; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | int strcmp(const char *s1, const char *s2) |
| 74 | { |
| 75 | while (1) { |
| 76 | if (*s1 != *s2) |
| 77 | return *s1 - *s2; |
| 78 | if (!*s1) |
| 79 | return 0; |
| 80 | s1++; |
| 81 | s2++; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | char *strchr(const char *s, int c) |
| 86 | { |
| 87 | while ((*s) && (*s != c)) |
| 88 | s++; |
| 89 | |
| 90 | if (*s == c) |
| 91 | return (char *)s; |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | void hexdump(unsigned char *start, int len) |
| 97 | { |
| 98 | int n; |
| 99 | |
| 100 | while (len > 0) { |
| 101 | print32((int)start); |
| 102 | (putc_func)(':'); |
| 103 | (putc_func)(' '); |
| 104 | for (n = 0; n < 16; n++) { |
| 105 | print8(*start++); |
| 106 | (putc_func)(' '); |
| 107 | } |
| 108 | (putc_func)('\n'); |
| 109 | len -= 16; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void setnybble(char *p, unsigned char n) |
| 114 | { |
| 115 | if (n < 10) |
| 116 | *p = '0' + n; |
| 117 | else |
| 118 | *p = 'a' + n - 10; |
| 119 | } |
| 120 | |
| 121 | void set8(char *p, unsigned char n) |
| 122 | { |
| 123 | setnybble(p, (n >> 4) & 15); |
| 124 | setnybble(p + 1, n & 15); |
| 125 | } |
| 126 | |
| 127 | void set32(char *p, unsigned int u) |
| 128 | { |
| 129 | set8(p, u >> 24); |
| 130 | set8(p + 2, u >> 16); |
| 131 | set8(p + 4, u >> 8); |
| 132 | set8(p + 6, u); |
| 133 | } |
| 134 | |
| 135 | |