Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use the talloc library for memory management.

  • Loading branch information...
commit 8824fe278141ebc49d35eb75e1f0cc1a10924257 1 parent e05db9a
Scott Olson authored
Showing with 14 additions and 16 deletions.
  1. +3 −2 Makefile
  2. +11 −14 src/bot.c
5 Makefile
View
@@ -2,8 +2,9 @@
CC := clang
OUTPUT := enfin
-CFLAGS := -std=gnu99 -D_GNU_SOURCE -MMD -Iinclude/ -Wall -Wextra
-LDFLAGS :=
+CFLAGS := -std=gnu99 -MMD -Iinclude/ -Wall -Wextra \
+ $(shell pkg-config --cflags talloc)
+LDFLAGS := $(shell pkg-config --libs talloc)
# Find sources
SOURCES := $(shell find src/ -name '*.c')
25 src/bot.c
View
@@ -8,29 +8,25 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
+#include <talloc.h>
#include "bot.h"
bot *bot_new(const char *nick, const char *username, const char *realname,
const char *hostname, const char *port) {
- bot *b = malloc(sizeof(bot));
+ bot *b = talloc(NULL, bot);
- b->nick = strdup(nick);
- b->username = strdup(username);
- b->realname = strdup(realname);
- b->hostname = strdup(hostname);
- b->port = strdup(port);
+ b->nick = talloc_strdup(b, nick);
+ b->username = talloc_strdup(b, username);
+ b->realname = talloc_strdup(b, realname);
+ b->hostname = talloc_strdup(b, hostname);
+ b->port = talloc_strdup(b, port);
return b;
}
void bot_free(bot *b) {
close(b->sock);
- free(b->nick);
- free(b->username);
- free(b->realname);
- free(b->hostname);
- free(b->port);
- free(b);
+ talloc_free(b);
}
void bot_connect(bot *b) {
@@ -121,9 +117,10 @@ void bot_sendf(bot *b, const char *format, ...) {
va_list args;
va_start(args, format);
- char *msg;
- vasprintf(&msg, format, args);
+ char *msg = talloc_vasprintf(NULL, format, args);
bot_send_raw(b, msg);
bot_send_raw(b, "\r\n");
+
+ talloc_free(msg);
}
Please sign in to comment.
Something went wrong with that request. Please try again.