frstr.c 646 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <mouse.h>
  6. #include <frame.h>
  7. #define CHUNK 16
  8. #define ROUNDUP(n) ((n+CHUNK)&~(CHUNK-1))
  9. uchar *
  10. _frallocstr(Frame *f, unsigned n)
  11. {
  12. uchar *p;
  13. p = malloc(ROUNDUP(n));
  14. if(p == 0)
  15. drawerror(f->display, "out of memory");
  16. return p;
  17. }
  18. void
  19. _frinsure(Frame *f, int bn, unsigned n)
  20. {
  21. Frbox *b;
  22. uchar *p;
  23. b = &f->box[bn];
  24. if(b->nrune < 0)
  25. drawerror(f->display, "_frinsure");
  26. if(ROUNDUP(b->nrune) > n) /* > guarantees room for terminal NUL */
  27. return;
  28. p = _frallocstr(f, n);
  29. b = &f->box[bn];
  30. memmove(p, b->ptr, NBYTE(b)+1);
  31. free(b->ptr);
  32. b->ptr = p;
  33. }