This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 8th, 2011  |  syntax: None  |  size: 3.35 KB  |  views: 66  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. diff --git a/firmware/core_alloc.c b/firmware/core_alloc.c
  2. index 2250f5c..85c52a1 100644
  3. --- a/firmware/core_alloc.c
  4. +++ b/firmware/core_alloc.c
  5. @@ -1,5 +1,6 @@
  6.  
  7.  #include <string.h>
  8. +#include <stdlib.h>
  9.  #include "core_alloc.h"
  10.  #include "buflib.h"
  11.  #include "buffer.h"
  12. @@ -15,14 +16,34 @@ void core_allocator_init(void)
  13.      buffer_release_buffer(size);
  14.  }
  15.  
  16. -int core_alloc(const char* name, size_t size)
  17. +int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks *ops)
  18.  {
  19. -    return buflib_alloc_ex(&core_ctx, size, name, NULL);
  20. +#ifdef APPLICATION
  21. +    int handle = buflib_alloc_ex(&core_ctx, sizeof(void*), name, ops);
  22. +    char* buffer = (char*)malloc(size);
  23. +    if (handle >= 0 && buffer)
  24. +    {
  25. +        char** data_ptr = core_get_data(handle);
  26. +        *data_ptr = buffer;
  27. +        printf("alloc: %d %x\n", handle, buffer);
  28. +        return CORE_TO_APP(handle);
  29. +    }
  30. +    else if (buffer)
  31. +    {
  32. +        free(buffer);
  33. +        handle = -1;
  34. +    }
  35. +    else
  36. +        buflib_free(&core_ctx, handle);
  37. +#else
  38. +    int handle = buflib_alloc_ex(&core_ctx, size, name, ops);
  39. +#endif
  40. +    return handle;
  41.  }
  42.  
  43. -int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks *ops)
  44. +int core_alloc(const char* name, size_t size)
  45.  {
  46. -    return buflib_alloc_ex(&core_ctx, size, name, ops);
  47. +    return core_alloc_ex(name, size, NULL);
  48.  }
  49.  
  50.  size_t core_available(void)
  51. @@ -32,6 +53,14 @@ size_t core_available(void)
  52.  
  53.  int core_free(int handle)
  54.  {
  55. +#ifdef APPLICATION
  56. +    if (IS_APP_HANDLE(handle))
  57. +    {
  58. +        handle = APP_TO_CORE(handle);
  59. +        char** buf = core_get_data(handle);
  60. +        free(*buf);
  61. +    }
  62. +#endif
  63.      return buflib_free(&core_ctx, handle);
  64.  }
  65.  
  66. @@ -42,6 +71,12 @@ int core_alloc_maximum(const char* name, size_t *size, struct buflib_callbacks *
  67.  
  68.  bool core_shrink(int handle, void* new_start, size_t new_size)
  69.  {
  70. +#ifdef APPLICATION
  71. +    if (IS_APP_HANDLE(handle))
  72. +    {
  73. +        return false;
  74. +    }
  75. +#endif
  76.      return buflib_shrink(&core_ctx, handle, new_start, new_size);
  77.  }
  78.  
  79. diff --git a/firmware/include/core_alloc.h b/firmware/include/core_alloc.h
  80. index f5206c9..b352526 100644
  81. --- a/firmware/include/core_alloc.h
  82. +++ b/firmware/include/core_alloc.h
  83. @@ -3,6 +3,7 @@
  84.  #define __CORE_ALLOC_H__
  85.  #include <string.h>
  86.  #include <stdbool.h>
  87. +#include <stdio.h>
  88.  #include "buflib.h"
  89.  
  90.  /* All functions below are wrappers for functions in buflib.h, except
  91. @@ -28,9 +29,26 @@ int  core_get_num_blocks(void);
  92.  void core_print_block_at(int block_num, char* buf, size_t bufsize);
  93.  #endif
  94.  
  95. +/* macros to help convert between application handles and core handles.
  96. + * 1<<31 would be a negative number so an error value, so use bit 30 to
  97. + * identify application handles. App handles use malloc() as the actual data
  98. + * store
  99. + */
  100. +#define APP_TO_CORE(h) ((h)&(~(1<<30)))
  101. +#define CORE_TO_APP(h) ((h)|(1<<30))
  102. +#define IS_APP_HANDLE(h) ((h)&(1<<30))
  103. +
  104.  static inline void* core_get_data(int handle)
  105.  {
  106.      extern struct buflib_context core_ctx;
  107. +#ifdef APPLICATION
  108. +    if (IS_APP_HANDLE(handle))
  109. +    {
  110. +        char **buf = buflib_get_data(&core_ctx, APP_TO_CORE(handle));
  111. +        printf("get %d %x\n", APP_TO_CORE(handle), *buf);
  112. +        return *buf;
  113. +    }
  114. +#endif
  115.      return buflib_get_data(&core_ctx, handle);
  116.  }
  117.  #endif /* __CORE_ALLOC_H__ */
clone this paste RAW Paste Data