akaros/user/ndblib/ndbfree.c
<<
>>
Prefs
   1/* 
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9#include <stdlib.h>
  10
  11#include <stdio.h>
  12#include <parlib/parlib.h>
  13#include <unistd.h>
  14#include <signal.h>
  15#include <fcntl.h>
  16#include <iplib/iplib.h>
  17#include <ndblib/ndb.h>
  18
  19/*
  20 *  free a parsed entry
  21 */
  22void ndbfree(struct ndbtuple *t)
  23{
  24        struct ndbtuple *tn;
  25
  26        for(; t; t = tn){
  27                tn = t->entry;
  28                if(t->val != t->valbuf){
  29                        free(t->val);
  30                }
  31                free(t);
  32        }
  33}
  34
  35/*
  36 *  set a value in a tuple
  37 */
  38void ndbsetval(struct ndbtuple *t, char *val, int n)
  39{
  40        if(n < Ndbvlen){
  41                if(t->val != t->valbuf){
  42                        free(t->val);
  43                        t->val = t->valbuf;
  44                }
  45        } else {
  46                if(t->val != t->valbuf)
  47                        t->val = realloc(t->val, n+1);
  48                else
  49                        t->val = calloc(n + 1, 1);
  50                if(t->val == NULL){
  51                        fprintf(stderr, "ndbsetval %r");
  52                        exit(1);
  53                }
  54        }
  55        strncpy(t->val, val, n);
  56        t->val[n] = 0;
  57}
  58
  59/*
  60 *  allocate a tuple
  61 */
  62struct ndbtuple *ndbnew(char *attr, char *val)
  63{
  64        struct ndbtuple *t;
  65
  66        t = calloc(1, sizeof(*t));
  67        if(t == NULL){
  68                fprintf(stderr, "ndbnew %r");
  69                exit(1);
  70        }
  71        if(attr != NULL)
  72                strncpy(t->attr, attr, sizeof(t->attr)-1);
  73        t->val = t->valbuf;
  74        if(val != NULL)
  75                ndbsetval(t, val, strlen(val));
  76        ndbsetmalloctag(t, getcallerpc(&attr));
  77        return t;       
  78}
  79
  80#if 0
  81/*
  82 *  set owner of a tuple
  83 */
  84void
  85ndbsetmalloctag(struct ndbtuple *t, uintptr_t tag)
  86{
  87        for(; t; t=t->entry)
  88                {}
  89}
  90#endif
  91