-
Per Cederqvist authoredPer Cederqvist authored
memory.c 6.69 KiB
/*
* $Id: memory.c,v 0.8 1992/02/26 18:45:07 ceder Exp $
* Copyright (C) 1991 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
*
* LysKOM is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* LysKOM is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with LysKOM; see the file COPYING. If not, write to
* Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
* or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
* MA 02139, USA.
*
* Please mail bug reports to bug-lyskom@lysator.liu.se.
*/
/*
* Memory allocators/deallocators.
*
* These functions should be used instead of smalloc/srealloc.
*/
static char *rcsid = "$Id: memory.c,v 0.8 1992/02/26 18:45:07 ceder Exp $";
#include <kom-types.h>
#include <string.h>
#include <server/smalloc.h>
#include "memory.h"
#include "exp.h"
#include "lyskomd.h"
#include "log.h"
static int person_cnt = 0;
static int conference_cnt = 0;
static int text_stat_cnt = 0;
EXPORT Person *
alloc_person(void)
{
Person *p;
person_cnt++;
p = smalloc(sizeof(Person));
*p = EMPTY_PERSON;
return p;
}
EXPORT Conference *
alloc_conference(void)
{
Conference *c;
conference_cnt++;
c = smalloc(sizeof(Conference));
*c = EMPTY_CONFERENCE;
return c;
}
EXPORT Text_stat *
alloc_text_stat(void)
{
Text_stat *t;
text_stat_cnt++;
t = smalloc(sizeof(Text_stat));
*t = EMPTY_TEXT_STAT;
return t;
}
static void
clear_text_list(Text_list *text_list)
{
if ( text_list == NULL )
{
log("clear_text_list(): text_list == NULL.\n");
return;
}
sfree(text_list->texts);
*text_list = EMPTY_TEXT_LIST;
}
static void
clear_mark_list(Mark_list *mark_list)
{
if ( mark_list == NULL )
{
log("clear_mark_list(): mark_list == NULL.\n");
return;
}
sfree(mark_list->marks);
*mark_list = EMPTY_MARK_LIST;
}
static void
clear_membership(Membership *mship)
{
if ( mship == NULL )
{
log("clear_membership(): mship == NULL.\n");
return;
}
sfree(mship->read_texts);
*mship = EMPTY_MEMBERSHIP;
}
static void
clear_membership_list(Membership_list *mlist)
{
int i;
if ( mlist == NULL )
{
log("clear_membership_list(): membership_list == NULL.\n");
return;
}
for ( i = 0; i < mlist->no_of_confs; i++ )
{
clear_membership(&mlist->confs[i]);
}
sfree(mlist->confs);
*mlist = EMPTY_MEMBERSHIP_LIST;
}
EXPORT void
clear_person(Person *person)
{
s_clear(&person->username);
clear_text_list(&person->created_texts);
clear_mark_list(&person->marks);
clear_membership_list(&person->conferences);
*person = EMPTY_PERSON;
}
EXPORT void
free_person(Person *person)
{
if ( person == NULL )
return;
person_cnt--;
clear_person(person);
sfree(person);
}
static void
clear_member_list(Member_list *m)
{
if ( m == NULL )
return;
sfree(m->members);
*m = EMPTY_MEMBER_LIST;
}
EXPORT void
clear_conference(Conference *confp)
{
s_clear(&confp->name);
clear_member_list(&confp->members);
clear_text_list(&confp->texts);
*confp = EMPTY_CONFERENCE;
}
EXPORT void
free_conference(Conference *confp)
{
if ( confp == NULL )
return;
conference_cnt--;
clear_conference(confp);
sfree(confp);
}
EXPORT void
clear_text_stat(Text_stat *t)
{
int i;
for ( i = 0; i < t->no_of_misc; i++ )
{
switch ( t->misc_items[ i ].type )
{
case recpt:
case cc_recpt:
case comm_to:
case comm_in:
case footn_to:
case footn_in:
case loc_no:
case rec_time:
case sent_by:
case sent_at:
/* No need to free anything for these. */
break;
default:
restart_kom(__FILE__ ": free_text_stat: unknown Info_type %d.",
t->misc_items[ i ].type );
}
}
sfree(t->misc_items);
*t = EMPTY_TEXT_STAT;
}
EXPORT void
free_text_stat(Text_stat *t)
{
if ( t == NULL )
return;
text_stat_cnt--;
clear_text_stat(t);
sfree(t);
}
static Text_list
copy_text_list(Text_list tl)
{
Text_list r;
r = tl;
r.texts = smalloc(r.no_of_texts * sizeof(Text_no));
memcpy(r.texts, tl.texts, r.no_of_texts * sizeof(Text_no));
return r;
}
static Mark_list
copy_mark_list(Mark_list ml)
{
Mark_list r;
r.no_of_marks = ml.no_of_marks;
r.marks = smalloc(r.no_of_marks * sizeof(Mark));
memcpy(r.marks, ml.marks, r.no_of_marks * sizeof(Mark));
return r;
}
static Membership
copy_membership(Membership m)
{
Membership res;
res = m;
res.read_texts = smalloc(m.no_of_read * sizeof(Local_text_no));
memcpy(res.read_texts, m.read_texts, m.no_of_read * sizeof(Local_text_no));
return res;
}
static Membership_list
copy_membership_list(Membership_list ml)
{
Membership_list r;
int i;
r.no_of_confs = ml.no_of_confs;
r.confs = smalloc(ml.no_of_confs * sizeof(Membership));
for ( i = 0; i < r.no_of_confs; i++ )
{
r.confs[i] = copy_membership(ml.confs[i]);
}
return r;
}
EXPORT Person *
copy_person(Person *p)
{
Person *c;
c = alloc_person();
*c = *p;
c->username = EMPTY_STRING;
s_strcpy(&c->username, p->username);
c->created_texts = copy_text_list(p->created_texts);
c->marks = copy_mark_list(p->marks);
c->conferences = copy_membership_list(p->conferences);
return c;
}
static Member_list
copy_member_list(Member_list ml)
{
Member_list res;
res.no_of_members = ml.no_of_members;
res.members = smalloc(res.no_of_members * sizeof ( Member ));
memcpy(res.members, ml.members, res.no_of_members * sizeof ( Member ));
return res;
}
EXPORT Conference *
copy_conf(Conference *o)
{
Conference *c;
c = alloc_conference();
*c = *o;
c->name = EMPTY_STRING;
s_strcpy(&c->name, o->name);
c->members = copy_member_list(o->members);
c->texts = copy_text_list(o->texts);
return c;
}
EXPORT Text_stat *
copy_text_stat(Text_stat *t)
{
Text_stat *c;
c = alloc_text_stat();
*c = *t;
c->misc_items = smalloc(c->no_of_misc * sizeof(Misc_info));
memcpy(c->misc_items, t->misc_items, c->no_of_misc * sizeof(Misc_info));
return c;
}
EXPORT void
dump_alloc_counts(FILE *fp)
{
fprintf(fp, "---memory.c:\n"
"\tperson: %d\n"
"\tconference: %d\n"
"\ttext_stat: %d\n",
person_cnt,
conference_cnt,
text_stat_cnt);
}