libc.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * libc.c
  3. *
  4. * Copyright (C) 2018 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * 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
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief Example bztalloc integration with an arbitrary libc
  27. */
  28. /* this should be provided by the linker script, and must be page aligned */
  29. extern char _dynbss_start;
  30. /* these must be implemented in your libc */
  31. extern void memzero (void *dest, size_t n);
  32. extern void *memcpy (void *dest, void *src, size_t n);
  33. /* POSIX compliant interface. Should call vmm_alloc() in your kernel through a system call. */
  34. extern void *mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offs);
  35. extern int munmap (void *addr, size_t len);
  36. /* the bss must have at least one empty page mapped in. Could be provided by the run-time linker
  37. * or the page fault handler too. Your choice. */
  38. void libc_init_malloc()
  39. {
  40. mmap((void*)_dynbss_start, PAGESIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
  41. memzero((void*)_dynbss_start, PAGESIZE);
  42. }
  43. /* defines required by bztalloc. Single threaded, no shared memory. See README.md for more options. */
  44. #define PAGESIZE 4096
  45. #define seterr(x) errno=x
  46. #define lockacquire(b,p)
  47. #define lockrelease(b,p)
  48. /* memory allocation in your libc, provided by bztalloc. You can use static functions instead if you'd like */
  49. #define malloc(s) bzt_alloc((void*)_dynbss_start,8,NULL,s,MAP_PRIVATE)
  50. #define calloc(n,s) bzt_alloc((void*)_dynbss_start,8,NULL,n*s,MAP_PRIVATE)
  51. #define realloc(p,s) bzt_alloc((void*)_dynbss_start,8,p,s,MAP_PRIVATE)
  52. #define aligned_alloc(a,s) bzt_alloc((void*)_dynbss_start,a,NULL,s,MAP_PRIVATE)
  53. #define free(p) bzt_free ((void*)_dynbss_start,p)