xref: /plan9-contrib/sys/src/libframe/frstr.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <libg.h>
4 #include <frame.h>
5 
6 /*
7  * The code here and elsewhere requires that strings not be gcalloc()ed
8  */
9 
10 #define	CHUNK	16
11 #define	ROUNDUP(n)	((n+CHUNK)&~(CHUNK-1))
12 
13 uchar *
14 _frallocstr(unsigned n)
15 {
16 	uchar *p;
17 
18 	p = malloc(ROUNDUP(n));
19 	if(p == 0)
20 		berror("out of memory");
21 	return p;
22 }
23 
24 void
25 _frinsure(Frame *f, int bn, unsigned n)
26 {
27 	Frbox *b;
28 	uchar *p;
29 
30 	b = &f->box[bn];
31 	if(b->nrune < 0)
32 		berror("_frinsure");
33 	if(ROUNDUP(b->nrune) > n)	/* > guarantees room for terminal NUL */
34 		return;
35 	p = _frallocstr(n);
36 	b = &f->box[bn];
37 	memmove(p, b->ptr, NBYTE(b)+1);
38 	free(b->ptr);
39 	b->ptr = p;
40 }
41