xref: /plan9/sys/src/libframe/frstr.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
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 
8 #define	CHUNK	16
9 #define	ROUNDUP(n)	((n+CHUNK)&~(CHUNK-1))
10 
11 uchar *
_frallocstr(Frame * f,unsigned n)12 _frallocstr(Frame *f, unsigned n)
13 {
14 	uchar *p;
15 
16 	p = malloc(ROUNDUP(n));
17 	if(p == 0)
18 		drawerror(f->display, "out of memory");
19 	return p;
20 }
21 
22 void
_frinsure(Frame * f,int bn,unsigned n)23 _frinsure(Frame *f, int bn, unsigned n)
24 {
25 	Frbox *b;
26 	uchar *p;
27 
28 	b = &f->box[bn];
29 	if(b->nrune < 0)
30 		drawerror(f->display, "_frinsure");
31 	if(ROUNDUP(b->nrune) > n)	/* > guarantees room for terminal NUL */
32 		return;
33 	p = _frallocstr(f, n);
34 	b = &f->box[bn];
35 	memmove(p, b->ptr, NBYTE(b)+1);
36 	free(b->ptr);
37 	b->ptr = p;
38 }
39