Mem cleanup
This commit is contained in:
parent
9fb4a9f5cd
commit
dcff9e437c
9
db.c
9
db.c
@ -41,6 +41,15 @@ static const char *const stmts[STMT__MAX] = {
|
|||||||
"SELECT books.id AS id,title FROM books LIMIT ?",
|
"SELECT books.id AS id,title FROM books LIMIT ?",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
db_book_free(struct book *b)
|
||||||
|
{
|
||||||
|
if (NULL == b)
|
||||||
|
return;
|
||||||
|
free(b->title);
|
||||||
|
free(b);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
db_books_load(struct kreq *r, struct book **books, int limit)
|
db_books_load(struct kreq *r, struct book **books, int limit)
|
||||||
{
|
{
|
||||||
|
1
extern.h
1
extern.h
@ -29,6 +29,7 @@ __BEGIN_DECLS
|
|||||||
|
|
||||||
struct book * db_book_load(struct kreq *r, int64_t id);
|
struct book * db_book_load(struct kreq *r, int64_t id);
|
||||||
int db_books_load(struct kreq *r, struct book **books, int limit);
|
int db_books_load(struct kreq *r, struct book **books, int limit);
|
||||||
|
void db_book_free(struct book *b);
|
||||||
|
|
||||||
void db_close(struct kreq *);
|
void db_close(struct kreq *);
|
||||||
int db_open(struct kreq *, const char *);
|
int db_open(struct kreq *, const char *);
|
||||||
|
6
main.c
6
main.c
@ -125,13 +125,17 @@ sendbooks(struct kreq *r)
|
|||||||
puterror(&req, "Unknown book");
|
puterror(&req, "Unknown book");
|
||||||
} else if (NULL != b) {
|
} else if (NULL != b) {
|
||||||
putbook(&req, b);
|
putbook(&req, b);
|
||||||
|
db_book_free(b);
|
||||||
} else {
|
} else {
|
||||||
kjson_array_open(&req);
|
kjson_array_open(&req);
|
||||||
struct book **books = kcalloc(10, sizeof(struct book));
|
struct book **books = kcalloc(10, sizeof(struct book));
|
||||||
res = db_books_load(r, books, 10);
|
res = db_books_load(r, books, 10);
|
||||||
while (i < res) {
|
while (i < res) {
|
||||||
putbook(&req, books[i++]);
|
putbook(&req, books[i]);
|
||||||
|
db_book_free(books[i]);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
free(books);
|
||||||
kjson_array_close(&req);
|
kjson_array_close(&req);
|
||||||
}
|
}
|
||||||
kjson_close(&req);
|
kjson_close(&req);
|
||||||
|
Loading…
Reference in New Issue
Block a user