Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. /* perform allocations in 4k chunks; as FreeBASIC only runs on x86, this
  2.  * allows for (hopefully) efficent memory allocation since we're allocating
  3.  * an entire page at once.
  4.  */
  5.  
  6. void FBSTRING::init_FBSTRING(void *str, int length);
  7. {
  8.     int m;
  9.     m = length / 4096;
  10.     if((length % 4096) > 0)
  11.         m++;
  12.     size = m * 4096;   
  13.     data = ::malloc(size);
  14.     len = length;
  15.     ::memcpy(data, str, length);
  16.     (char *) data[length] = 0;
  17. }
  18.  
  19. void FBSTRING::fini_FBSTRING()
  20. {
  21.     if(data) free(data);
  22.     data = 0;
  23.     len = 0;
  24.     size = 0;
  25. }
  26.  
  27. /* initialize an empty string */
  28. FBSTRING::FBSTRING()
  29. {
  30.     data = 0;
  31.     len = 0;
  32.     size = 0;
  33. }
  34.  
  35. /* initialize a string from a character array */
  36. FBSTRING::FBSTRING(const char *s)
  37. {
  38.     data = 0;
  39.     len = 0;
  40.     size = 0;
  41.     if(s == 0) return;
  42.     int s_len = strlen(s);
  43.     init_FBSTRING((void *) s, s_len);
  44. }
  45.  
  46. /* initialize a string from a character array, with a given starting offset. */
  47. FBSTRING::FBSTRING(const char *s, const size_t l)
  48. {
  49.     data = 0;
  50.     len = 0;
  51.     size = 0;
  52.     if(s == 0) return;
  53.     int s_len = strlen(s);
  54.     if (s_len < (int) l) return;
  55.     int m = s_len - (int) l;
  56.     init_FBSTRING((void *) s + (void *) l, m);
  57. }
  58.  
  59. /* initialize a string from a character array, with a given starting offset
  60.  * plus max length */
  61. FBSTRING::FBSTRING(const char *s, const size_t l, const size_t x)
  62. {
  63.     data = 0;
  64.     len = 0;
  65.     size = 0;
  66.     if((s == 0) || (x == 0)) return;
  67.     int s_len = strlen(s);
  68.     if (s_len < (int) l) return;
  69.     int m = s_len - (int) l;
  70.     init_FBSTRING((void *) s + (void *) l, m < x ? m : x);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement