fix some alignment issues and modernize file a little bit

git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1535 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2007-07-25 16:33:56 +00:00
parent 929526de33
commit 98d36efa82

View file

@ -27,12 +27,12 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include <pulsecore/macro.h>
#include "strbuf.h" #include "strbuf.h"
@ -42,7 +42,7 @@ struct chunk {
size_t length; size_t length;
}; };
#define CHUNK_TO_TEXT(c) ((char*) (c) + sizeof(struct chunk)) #define CHUNK_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(struct chunk)))
struct pa_strbuf { struct pa_strbuf {
size_t length; size_t length;
@ -50,14 +50,17 @@ struct pa_strbuf {
}; };
pa_strbuf *pa_strbuf_new(void) { pa_strbuf *pa_strbuf_new(void) {
pa_strbuf *sb = pa_xmalloc(sizeof(pa_strbuf));
pa_strbuf *sb = pa_xnew(pa_strbuf, 1);
sb->length = 0; sb->length = 0;
sb->head = sb->tail = NULL; sb->head = sb->tail = NULL;
return sb; return sb;
} }
void pa_strbuf_free(pa_strbuf *sb) { void pa_strbuf_free(pa_strbuf *sb) {
assert(sb); pa_assert(sb);
while (sb->head) { while (sb->head) {
struct chunk *c = sb->head; struct chunk *c = sb->head;
sb->head = sb->head->next; sb->head = sb->head->next;
@ -72,9 +75,10 @@ void pa_strbuf_free(pa_strbuf *sb) {
char *pa_strbuf_tostring(pa_strbuf *sb) { char *pa_strbuf_tostring(pa_strbuf *sb) {
char *t, *e; char *t, *e;
struct chunk *c; struct chunk *c;
assert(sb);
pa_assert(sb);
e = t = pa_xmalloc(sb->length+1); e = t = pa_xnew(char, sb->length+1);
for (c = sb->head; c; c = c->next) { for (c = sb->head; c; c = c->next) {
assert((size_t) (e-t) <= sb->length); assert((size_t) (e-t) <= sb->length);
@ -93,27 +97,33 @@ char *pa_strbuf_tostring(pa_strbuf *sb) {
/* Combination of pa_strbuf_free() and pa_strbuf_tostring() */ /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
char *pa_strbuf_tostring_free(pa_strbuf *sb) { char *pa_strbuf_tostring_free(pa_strbuf *sb) {
char *t; char *t;
assert(sb);
pa_assert(sb);
t = pa_strbuf_tostring(sb); t = pa_strbuf_tostring(sb);
pa_strbuf_free(sb); pa_strbuf_free(sb);
return t; return t;
} }
/* Append a string to the string buffer */ /* Append a string to the string buffer */
void pa_strbuf_puts(pa_strbuf *sb, const char *t) { void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
assert(sb && t);
pa_assert(sb);
pa_assert(t);
pa_strbuf_putsn(sb, t, strlen(t)); pa_strbuf_putsn(sb, t, strlen(t));
} }
/* Append a new chunk to the linked list */ /* Append a new chunk to the linked list */
static void append(pa_strbuf *sb, struct chunk *c) { static void append(pa_strbuf *sb, struct chunk *c) {
assert(sb && c); pa_assert(sb);
pa_assert(c);
if (sb->tail) { if (sb->tail) {
assert(sb->head); pa_assert(sb->head);
sb->tail->next = c; sb->tail->next = c;
} else { } else {
assert(!sb->head); pa_assert(!sb->head);
sb->head = c; sb->head = c;
} }
@ -125,12 +135,14 @@ static void append(pa_strbuf *sb, struct chunk *c) {
/* Append up to l bytes of a string to the string buffer */ /* Append up to l bytes of a string to the string buffer */
void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) { void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
struct chunk *c; struct chunk *c;
assert(sb && t);
pa_assert(sb);
pa_assert(t);
if (!l) if (!l)
return; return;
c = pa_xmalloc(sizeof(struct chunk)+l); c = pa_xmalloc(PA_ALIGN(sizeof(struct chunk)) + l);
c->length = l; c->length = l;
memcpy(CHUNK_TO_TEXT(c), t, l); memcpy(CHUNK_TO_TEXT(c), t, l);
@ -143,13 +155,14 @@ int pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
int size = 100; int size = 100;
struct chunk *c = NULL; struct chunk *c = NULL;
assert(sb); pa_assert(sb);
pa_assert(format);
for(;;) { for(;;) {
va_list ap; va_list ap;
int r; int r;
c = pa_xrealloc(c, sizeof(struct chunk)+size); c = pa_xrealloc(c, PA_ALIGN(sizeof(struct chunk)) + size);
va_start(ap, format); va_start(ap, format);
r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap); r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);