int main(int argc, char *argv[]) { void *p = NULL; void *p2 = NULL; void *p3 = NULL; void *p4 = NULL; p = priv_malloc(1024); print_item(); fprintf(stdout, "alloc %d bytes, %p\n", 1024, p); p2 = priv_malloc(1024); print_item(); fprintf(stdout, "alloc %d bytes, %p\n", 1024, p2); p3 = priv_malloc(1024); print_item(); fprintf(stdout, "alloc %d bytes, %p\n", 1024, p3); p4 = priv_malloc(1024); print_item(); fprintf(stdout, "alloc %d bytes, %p\n", 1024, p4); fprintf(stdout, "free %p\n", p); priv_free(p); print_item(); fprintf(stdout, "free %p\n", p2); priv_free(p2); print_item(); fprintf(stdout, "free %p\n", p3); priv_free(p3); print_item(); fprintf(stdout, "free %p\n", p4); priv_free(p4); print_item(); exit(0); }
/*}}}*/ } priv_t; static char * xfree (char *s) /*{{{*/ { if (s) free (s); return NULL; }/*}}}*/ static bool_t xcopy (char **buf, const char *str) /*{{{*/ { if (*buf) free (*buf); *buf = str ? strdup (str) : NULL; return (! str) || *buf ? true : false; }/*}}}*/ static void priv_clear (priv_t *p) /*{{{*/ { if (p) { p -> x_agn = 0; p -> from = xfree (p -> from); p -> receiver = charc_free_all (p -> receiver); p -> prev = NULL; p -> info = xfree (p -> info); } }/*}}}*/ static priv_t * priv_free (priv_t *p) /*{{{*/ { if (p) { priv_clear (p); if (p -> lg) log_free (p -> lg); if (p -> cfg) cfg_free (p -> cfg); free (p); } return NULL; }/*}}}*/ static priv_t * priv_alloc (void) /*{{{*/ { priv_t *p; if (p = (priv_t *) malloc (sizeof (priv_t))) if (p -> cfg = cfg_alloc (cfgfile)) { p -> is_local = false; p -> x_agn = 0; p -> from = NULL; p -> receiver = NULL; p -> prev = NULL; p -> info = NULL; if (! (p -> lg = log_alloc (NULL, program, loglevel))) p = priv_free (p); } else { free (p); p = NULL; } return p; }/*}}}*/ static bool_t priv_setfrom (priv_t *p, const char *from) /*{{{*/ { return xcopy (& p -> from, from); }/*}}}*/ static bool_t priv_setto (priv_t *p, const char *to) /*{{{*/ { charc_t *r; if (r = charc_alloc (to)) { if (p -> prev) p -> prev -> next = r; else p -> receiver = r; p -> prev = r; } return r ? true : false; }/*}}}*/ static bool_t priv_addinfo (priv_t *p, const char *info) /*{{{*/ { char *temp; if ((! p -> info) || (! p -> info[0])) return xcopy (& p -> info, info); if (temp = malloc (strlen (p -> info) + strlen (info) + 2)) { sprintf (temp, "%s,%s", p -> info, info); free (p -> info); p -> info = temp; return true; } return false; }/*}}}*/ static bool_t priv_addinfopair (priv_t *p, const char *var, const char *val) /*{{{*/ { bool_t rc; char *scratch, *ptr; if (scratch = malloc (strlen (var) + strlen (val) + 2)) { for (ptr = scratch; *var; *ptr++ = *var++) ; *ptr++ = '='; for (;*val; ++val) *ptr++ = *val == ',' ? '_' : *val; *ptr = '\0'; rc = priv_addinfo (p, scratch); free (scratch); } else rc = false; return rc; }/*}}}*/ static sfsistat handle_connect (SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) /*{{{*/ { priv_t *p; if (! (p = priv_alloc ())) return SMFIS_TEMPFAIL; if (hostaddr -> sa_family == AF_INET) { struct sockaddr_in *iaddr = (struct sockaddr_in *) hostaddr; if (ntohl (iaddr -> sin_addr.s_addr) == INADDR_LOOPBACK) p -> is_local = true; } # ifdef AF_INET6 else if (hostaddr -> sa_family == AF_INET6) { struct sockaddr_in6 *i6addr = (struct sockaddr_in6 *) hostaddr; static struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT; if (memcmp (& i6addr -> sin6_addr, & loopback, sizeof (i6addr -> sin6_addr)) == 0) p -> is_local = true; } # endif /* AF_INET6 */ smfi_setpriv (ctx, p); return SMFIS_CONTINUE; }/*}}}*/ static sfsistat handle_from (SMFICTX *ctx, char **argv) /*{{{*/ { priv_t *p = (priv_t *) smfi_getpriv (ctx); if (! p) return SMFIS_TEMPFAIL; priv_clear (p); if (! priv_setfrom (p, argv[0])) return SMFIS_TEMPFAIL; priv_addinfopair (p, "from", argv[0]); return SMFIS_CONTINUE; }/*}}}*/ static sfsistat handle_to (SMFICTX *ctx, char **argv) /*{{{*/ { priv_t *p = (priv_t *) smfi_getpriv (ctx); char *chk, *opt; bool_t reject, tempfail; if (! p) return SMFIS_TEMPFAIL; if (p -> is_local) return SMFIS_CONTINUE; if (! (chk = cfg_valid_address (p -> cfg, argv[0]))) { log_out (p -> lg, LV_ERROR, "Unable to setup initial data for `%s'", argv[0]); return SMFIS_TEMPFAIL; } if (opt = strchr (chk, ':')) *opt++ = '\0'; reject = false; tempfail = false; if (! strcmp (chk, ID_REJECT)) reject = true; else if (! strcmp (chk, ID_TEMPFAIL)) tempfail = true; else if ((! strcmp (chk, ID_ACCEPT)) && opt) priv_addinfo (p, opt); priv_addinfopair (p, "to", argv[0]); free (chk); if (reject) { log_out (p -> lg, LV_INFO, "Receiver `%s' is rejected", argv[0]); smfi_setreply (ctx, (char *) "550", (char *) "5.1.1", (char *) "No such user"); return SMFIS_REJECT; } if (tempfail) { log_out (p -> lg, LV_INFO, "Receiver `%s' is temp. disbaled", argv[0]); smfi_setreply (ctx, (char *) "400", (char *) "4.0.0", (char *) "Please try again later"); return SMFIS_TEMPFAIL; } if (! priv_setto (p, argv[0])) return SMFIS_TEMPFAIL; return SMFIS_CONTINUE; }/*}}}*/ static sfsistat handle_header (SMFICTX *ctx, char *field, char *value) /*{{{*/ { priv_t *p = (priv_t *) smfi_getpriv (ctx); if (! p) return SMFIS_TEMPFAIL; if (p -> is_local) return SMFIS_CONTINUE; if (! strcasecmp (field, X_LOOP)) { log_out (p -> lg, LV_WARNING, "Mail from `%s' has already loop marker set, rejected", p -> from); smfi_setreply (ctx, (char *) "500", (char *) "5.4.6", (char *) "Loop detected"); return SMFIS_REJECT; } if (! strcasecmp (field, X_AGN)) p -> x_agn++; return SMFIS_CONTINUE; }/*}}}*/ static sfsistat handle_eom (SMFICTX *ctx) /*{{{*/ { priv_t *p = (priv_t *) smfi_getpriv (ctx); int n; if (! p) return SMFIS_TEMPFAIL; for (n = 0; n < p -> x_agn; ++n) smfi_chgheader (ctx, (char *) X_AGN, 0, NULL); if (! p -> is_local) { if (p -> info) smfi_addheader (ctx, (char *) X_AGN, p -> info); smfi_addheader (ctx, (char *) X_LOOP, (char *) LOOP_SET); } return SMFIS_CONTINUE; }/*}}}*/ static sfsistat handle_close(SMFICTX *ctx) /*{{{*/ { priv_free (smfi_getpriv (ctx)); smfi_setpriv (ctx, NULL); return SMFIS_CONTINUE; }/*}}}*/
static ret_t base_free (cherokee_collector_base_t *collector) { priv_free (collector->priv); free (collector); return ret_ok; }
unsigned int release(Memory mem, void* object) { unsigned int* t = object_to_refcount(object); *t = *t - 1; if (*t == 0) { priv_free(mem, t); return *t; } return *t; }
void test_priv_free(void) { Manual mem = (Manual) priv_imalloc(1 Mb, MANUAL + ASCENDING_SIZE); Priv_mem new_mem = style_to_priv((Memory) mem); mem->alloc((Memory) mem, 1 Kb); void *temp = mem->alloc((Memory) mem, 2 Kb); mem->alloc((Memory) mem, 1 Kb); CU_ASSERT(memory_start(search_memory(temp, new_mem->lists->alloclist, FALSE)) == temp); CU_ASSERT(priv_free((Memory) mem, temp) == 2 Kb); CU_ASSERT(memory_start(search_memory(temp, new_mem->lists->alloclist, FALSE)) == NULL); free_lists(new_mem->lists); free(new_mem->as->start); free(new_mem); }
void test_avail(void) { Manual mem = (Manual) priv_imalloc(1 Mb, MANUAL + ASCENDING_SIZE); mem->alloc((Memory) mem, 1 Kb); void *temp = mem->alloc((Memory) mem, 1 Kb); mem->alloc((Memory) mem, 1 Kb); CU_ASSERT(priv_free((Memory) mem, temp) == 1 Kb); CU_ASSERT(avail((Memory) mem) == 1 Mb - 3 * 1 Kb); mem->alloc((Memory) mem, 1 Mb - 3 * 1 Kb); CU_ASSERT(avail((Memory) mem) == 1 Kb); Priv_mem new_mem = style_to_priv((Memory) mem); free_lists(new_mem->lists); free(new_mem->as->start); free(new_mem); }
static union Header *morecore(size_t nunits) { union Header *p = NULL; void *ap = NULL; if (nunits < NALLOC) nunits = NALLOC; ap = sbrk(nunits * sizeof(*p)); if (ap == (char *)-1) { return NULL; } p = (union Header *)ap; p->s.size = nunits; priv_free((void *)(p + 1)); return freep; }
static union Header *morecore(size_t nunits) { union Header *p = NULL; void *ap = NULL; if (nunits < NALLOC) nunits = NALLOC; // fprintf(stdout, "%s: calling more memroy: %d\n", __func__, nunits * sizeof(*p)); ap = sbrk(nunits * sizeof(*p)); if (ap == (char *)-1) { return NULL; } // fprintf(stdout, "%s: get %d bytes\n", __func__, nunits * sizeof(*p)); p = (union Header *)ap; p->s.size = nunits; priv_free((void *)(p + 1)); return freep; }
/*}}}*/ } priv_t; static char * xfree (char *s) /*{{{*/ { if (s) free (s); return NULL; }/*}}}*/ static bool_t xcopy (char **buf, const char *str) /*{{{*/ { if (*buf) free (*buf); *buf = str ? strdup (str) : NULL; return (! str) || *buf ? true : false; }/*}}}*/ static void priv_clear (priv_t *p) /*{{{*/ { if (p) { p -> x_agn = 0; p -> from = xfree (p -> from); p -> receiver = charc_free_all (p -> receiver); p -> prev = NULL; p -> info = xfree (p -> info); } }/*}}}*/ static priv_t * priv_free (priv_t *p) /*{{{*/ { if (p) { priv_clear (p); if (p -> lg) log_free (p -> lg); if (p -> cfg) cfg_free (p -> cfg); free (p); } return NULL; }/*}}}*/ static priv_t * priv_alloc (void) /*{{{*/ { priv_t *p; if (p = (priv_t *) malloc (sizeof (priv_t))) if (p -> cfg = cfg_alloc (cfgfile)) { p -> is_local = false; p -> x_agn = 0; p -> from = NULL; p -> receiver = NULL; p -> prev = NULL; p -> info = NULL; if (! (p -> lg = log_alloc (NULL, program, loglevel))) p = priv_free (p); } else { free (p); p = NULL; } return p; }/*}}}*/