frstr.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <thread.h>
  13. #include <mouse.h>
  14. #include <frame.h>
  15. #define CHUNK 16
  16. #define ROUNDUP(n) ((n+CHUNK)&~(CHUNK-1))
  17. uint8_t *
  18. _frallocstr(Frame *f, unsigned n)
  19. {
  20. uint8_t *p;
  21. p = malloc(ROUNDUP(n));
  22. if(p == 0)
  23. drawerror(f->display, "out of memory");
  24. return p;
  25. }
  26. void
  27. _frinsure(Frame *f, int bn, unsigned n)
  28. {
  29. Frbox *b;
  30. uint8_t *p;
  31. b = &f->box[bn];
  32. if(b->nrune < 0)
  33. drawerror(f->display, "_frinsure");
  34. if(ROUNDUP(b->nrune) > n) /* > guarantees room for terminal NUL */
  35. return;
  36. p = _frallocstr(f, n);
  37. b = &f->box[bn];
  38. memmove(p, b->ptr, NBYTE(b)+1);
  39. free(b->ptr);
  40. b->ptr = p;
  41. }