Clear up confusion between lengths and sizes.

Fix a harmless bug in string_shrink().
This commit is contained in:
Dag-Erling Smørgrav 2015-10-13 21:42:57 +00:00 committed by des
parent 0a9f2c21fe
commit fd515ccd79

View file

@ -28,10 +28,12 @@
*/ */
/* size of static buffer used for short strings */ /* size of static buffer used for short strings */
#define STATIC_BUF_SIZE (16 * sizeof(char_t)) #define STATIC_BUF_LEN 16
#define STATIC_BUF_SIZE (STATIC_BUF_LEN * sizeof(char_t))
/* threshold at which we switch from exponential to linear growth */ /* threshold at which we switch from exponential to linear growth */
#define LARGE_BUF_SIZE (4096 * sizeof(char_t)) #define LARGE_BUF_LEN 4096
#define LARGE_BUF_SIZE (LARGE_BUF_LEN * sizeof(char_t))
/* minimum buffer size to store len characters + terminating zero */ /* minimum buffer size to store len characters + terminating zero */
#define L2S(len) (((len) + 1) * sizeof(char_t)) #define L2S(len) (((len) + 1) * sizeof(char_t))
@ -46,7 +48,7 @@ struct cryb_string {
char_t *buf; /* pointer to buffer */ char_t *buf; /* pointer to buffer */
size_t size; /* size of buffer in bytes */ size_t size; /* size of buffer in bytes */
size_t len; /* length of string in characters */ size_t len; /* length of string in characters */
char_t staticbuf[STATIC_BUF_SIZE]; char_t staticbuf[STATIC_BUF_LEN];
}; };
/* /*
@ -190,6 +192,8 @@ string_shrink(string *str)
newbuf = realloc(str->buf, newsize); newbuf = realloc(str->buf, newsize);
} else { } else {
newsize = LARGE_BUF_SIZE; newsize = LARGE_BUF_SIZE;
while (newsize / 2 > L2S(str->len))
newsize = newsize / 2;
newbuf = realloc(str->buf, newsize); newbuf = realloc(str->buf, newsize);
} }
str->buf = newbuf; str->buf = newbuf;