Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use the talloc library for memory management.

  • Loading branch information...
commit 8824fe278141ebc49d35eb75e1f0cc1a10924257 1 parent e05db9a
Scott Olson authored

Showing 2 changed files with 14 additions and 16 deletions. Show diff stats Hide diff stats

  1. +3 2 Makefile
  2. +11 14 src/bot.c
5 Makefile
@@ -2,8 +2,9 @@
2 2
3 3 CC := clang
4 4 OUTPUT := enfin
5   -CFLAGS := -std=gnu99 -D_GNU_SOURCE -MMD -Iinclude/ -Wall -Wextra
6   -LDFLAGS :=
  5 +CFLAGS := -std=gnu99 -MMD -Iinclude/ -Wall -Wextra \
  6 + $(shell pkg-config --cflags talloc)
  7 +LDFLAGS := $(shell pkg-config --libs talloc)
7 8
8 9 # Find sources
9 10 SOURCES := $(shell find src/ -name '*.c')
25 src/bot.c
@@ -8,29 +8,25 @@
8 8 #include <sys/types.h>
9 9 #include <sys/socket.h>
10 10 #include <netdb.h>
  11 +#include <talloc.h>
11 12 #include "bot.h"
12 13
13 14 bot *bot_new(const char *nick, const char *username, const char *realname,
14 15 const char *hostname, const char *port) {
15   - bot *b = malloc(sizeof(bot));
  16 + bot *b = talloc(NULL, bot);
16 17
17   - b->nick = strdup(nick);
18   - b->username = strdup(username);
19   - b->realname = strdup(realname);
20   - b->hostname = strdup(hostname);
21   - b->port = strdup(port);
  18 + b->nick = talloc_strdup(b, nick);
  19 + b->username = talloc_strdup(b, username);
  20 + b->realname = talloc_strdup(b, realname);
  21 + b->hostname = talloc_strdup(b, hostname);
  22 + b->port = talloc_strdup(b, port);
22 23
23 24 return b;
24 25 }
25 26
26 27 void bot_free(bot *b) {
27 28 close(b->sock);
28   - free(b->nick);
29   - free(b->username);
30   - free(b->realname);
31   - free(b->hostname);
32   - free(b->port);
33   - free(b);
  29 + talloc_free(b);
34 30 }
35 31
36 32 void bot_connect(bot *b) {
@@ -121,9 +117,10 @@ void bot_sendf(bot *b, const char *format, ...) {
121 117 va_list args;
122 118 va_start(args, format);
123 119
124   - char *msg;
125   - vasprintf(&msg, format, args);
  120 + char *msg = talloc_vasprintf(NULL, format, args);
126 121
127 122 bot_send_raw(b, msg);
128 123 bot_send_raw(b, "\r\n");
  124 +
  125 + talloc_free(msg);
129 126 }

0 comments on commit 8824fe2

Please sign in to comment.
Something went wrong with that request. Please try again.