memory.c 13.3 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
David Byers's avatar
David Byers committed
2
 * $Id: memory.c,v 0.21 1997/10/23 12:37:25 byers Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
/*
26
 * Memory allocators/deallocators/initializers.
Per Cederqvist's avatar
Per Cederqvist committed
27 28 29 30
 *
 * These functions should be used instead of smalloc/srealloc.
 */

David Byers's avatar
David Byers committed
31
static char *rcsid = "$Id: memory.c,v 0.21 1997/10/23 12:37:25 byers Exp $";
32 33
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
34

Per Cederqvist's avatar
Per Cederqvist committed
35
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
36 37 38
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
39 40 41
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
42 43
#include <sys/types.h>
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
44

45
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
46 47 48
#include "kom-types.h"
#include "kom-memory.h"
#include "server/smalloc.h"
49
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
50
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
51 52 53 54 55

static int person_cnt = 0;
static int conference_cnt = 0;
static int text_stat_cnt = 0;

56
/* Forward declarations */
Per Cederqvist's avatar
Per Cederqvist committed
57

58 59 60 61 62 63 64 65 66 67
static void            clear_mark_list (Mark_list *mark_list);
static Mark_list       copy_mark_list  (Mark_list ml);
static void            clear_member_list (Member_list *m);
static Member_list     copy_member_list  (Member_list ml);
static void            clear_membership (Membership *mship);
static Membership      copy_membership  (Membership m);
static void            clear_membership_list (Membership_list *mlist);
static Membership_list copy_membership_list  (Membership_list ml);
static void            clear_text_list (Text_list *text_list);
static Text_list       copy_text_list  (Text_list tl);
Per Cederqvist's avatar
Per Cederqvist committed
68

69 70 71 72 73 74 75 76 77 78

/* Conf_type */

void
init_conf_type(Conf_type *ct)
{
    ct->rd_prot = 0;
    ct->original = 0;
    ct->secret = 0;
    ct->letter_box = 0;
Per Cederqvist's avatar
Per Cederqvist committed
79 80
}

81
/* Conference */
Per Cederqvist's avatar
Per Cederqvist committed
82

83
Conference *
Per Cederqvist's avatar
Per Cederqvist committed
84 85 86 87 88 89 90
alloc_conference(void)
{
    Conference *c;

    conference_cnt++;
    
    c = smalloc(sizeof(Conference));
91
    init_conference(c);
Per Cederqvist's avatar
Per Cederqvist committed
92 93 94
    return c;
}

95 96
void
free_conference(Conference *confp)
Per Cederqvist's avatar
Per Cederqvist committed
97
{
98 99
    if ( confp == NULL )
	return;
Per Cederqvist's avatar
Per Cederqvist committed
100

101 102 103
    conference_cnt--;
    clear_conference(confp);
    sfree(confp);
Per Cederqvist's avatar
Per Cederqvist committed
104 105
}

106 107
void
clear_conference(Conference *confp)
Per Cederqvist's avatar
Per Cederqvist committed
108
{
109 110 111
    s_clear(&confp->name);
    clear_member_list(&confp->members);
    clear_text_list(&confp->texts);
David Byers's avatar
David Byers committed
112
    free_aux_item_list(&confp->aux_item_list);
113 114
    init_conference(confp);
}
Per Cederqvist's avatar
Per Cederqvist committed
115

116 117 118 119 120 121 122 123 124 125 126
Conference *
copy_conference (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);
David Byers's avatar
David Byers committed
127 128
    copy_aux_item_list(&c->aux_item_list,
                       &o->aux_item_list);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    return c;
}

void
init_conference  (Conference *c)
{
    init_conf_type(&c->type);
    c->creator = 0;
    c->supervisor = 0;
    c->permitted_submitters = 0;
    c->super_conf = 0;
    c->creation_time = NO_TIME;
    c->presentation = 0;
    c->last_written = NO_TIME;
    c->msg_of_day = 0;
    c->nice = 0;
    c->name = EMPTY_STRING;
    init_member_list(&c->members);
    init_text_list(&c->texts);
David Byers's avatar
David Byers committed
148
    init_aux_item_list(&c->aux_item_list);
Per Cederqvist's avatar
Per Cederqvist committed
149 150
}

151 152 153 154 155 156 157 158 159
/* Dynamic_session_info */

void
init_dynamic_session_info (Dynamic_session_info *d)
{
    d->session = 0;
    d->person = 0;
    d->working_conference = 0;
    d->idle_time = 0;
160 161 162 163 164 165 166 167
    d->flags.invisible = FALSE;
    d->flags.user_active_used = FALSE;
    d->flags.user_absent = FALSE;
    d->flags.reserved3 = FALSE;
    d->flags.reserved4 = FALSE;
    d->flags.reserved5 = FALSE;
    d->flags.reserved6 = FALSE;
    d->flags.reserved7 = FALSE;
168 169 170
    d->what_am_i_doing = EMPTY_STRING;
}

171

172 173
/* Mark_list */

174 175
static  void
clear_mark_list(Mark_list *mark_list)
Per Cederqvist's avatar
Per Cederqvist committed
176 177
{
    if ( mark_list == NULL )
178 179
    {
	log("clear_mark_list(): mark_list == NULL.\n");
Per Cederqvist's avatar
Per Cederqvist committed
180
	return;
181
    }
Per Cederqvist's avatar
Per Cederqvist committed
182 183

    sfree(mark_list->marks);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    init_mark_list(mark_list);
}

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;
}

void
init_mark_list (Mark_list *ml)
{
    ml->no_of_marks = 0;
    ml->marks = NULL;
}

/* Member_list */

static void 
clear_member_list(Member_list *m)
{
    if ( m == NULL )
	return;

    sfree(m->members);
    init_member_list(m);
}

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;
Per Cederqvist's avatar
Per Cederqvist committed
226 227
}

228 229 230 231 232 233 234 235
void
init_member_list (Member_list *ml)
{
    ml->no_of_members = 0;
    ml->members = NULL;
}

/* Membership */
Per Cederqvist's avatar
Per Cederqvist committed
236

237 238 239 240
static  void
clear_membership(Membership *mship)
{
    if ( mship == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
241
    {
242 243
	log("clear_membership(): mship == NULL.\n");
	return;
Per Cederqvist's avatar
Per Cederqvist committed
244 245
    }

246
    sfree(mship->read_texts);
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    init_membership(mship);
}

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;
}

void
init_membership(Membership *m)
{
    m->conf_no = 0;
    m->priority = 0;
    m->last_text_read = 0;
    m->no_of_read = 0;
    m->read_texts = NULL;
    m->last_time_read = NO_TIME;
Per Cederqvist's avatar
Per Cederqvist committed
270 271
}

272 273
/* Membership_list */

Per Cederqvist's avatar
Per Cederqvist committed
274
static void
275
clear_membership_list(Membership_list *mlist)
Per Cederqvist's avatar
Per Cederqvist committed
276
{
277 278 279 280 281
    int i;

    if ( mlist == NULL )
    {
	log("clear_membership_list(): membership_list == NULL.\n");
Per Cederqvist's avatar
Per Cederqvist committed
282
	return;
283
    }
Per Cederqvist's avatar
Per Cederqvist committed
284

285 286 287 288
    for ( i = 0; i < mlist->no_of_confs; i++ )
    {
	clear_membership(&mlist->confs[i]);
    }
Per Cederqvist's avatar
Per Cederqvist committed
289

290
    sfree(mlist->confs);
291
    init_membership_list(mlist);
Per Cederqvist's avatar
Per Cederqvist committed
292 293
}

294 295 296 297 298 299 300 301
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));
302

303 304 305 306 307 308 309 310 311 312
    for ( i = 0; i < r.no_of_confs; i++ )
    {
	r.confs[i] = copy_membership(ml.confs[i]);
    }

    return r;
}

void
init_membership_list (Membership_list *m)
Per Cederqvist's avatar
Per Cederqvist committed
313
{
314 315 316 317
    m->no_of_confs = 0;
    m->confs = NULL;
}

318 319 320 321 322 323 324 325 326 327 328
/* Static_session_info */

void
init_static_session_info (Static_session_info *d)
{
    d->username = EMPTY_STRING;
    d->hostname = EMPTY_STRING;
    d->ident_user = EMPTY_STRING;
    d->connection_time = NO_TIME;
}

329 330 331 332 333 334 335 336 337 338 339
/* Person */
Person *
alloc_person(void)
{
    Person *p;

    person_cnt++;
    
    p = smalloc(sizeof(Person));
    init_person(p);
    return p;
Per Cederqvist's avatar
Per Cederqvist committed
340 341
}

342
void
Per Cederqvist's avatar
Per Cederqvist committed
343 344 345 346 347 348 349 350 351 352
free_person(Person *person)
{
    if ( person == NULL )
	return;

    person_cnt--;
    clear_person(person);
    sfree(person);
}

353 354 355 356 357 358 359 360 361
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);
    init_person(person);
}
Per Cederqvist's avatar
Per Cederqvist committed
362

363 364
Person *
copy_person(Person *p)
Per Cederqvist's avatar
Per Cederqvist committed
365
{
366
    Person *c;
Per Cederqvist's avatar
Per Cederqvist committed
367

368 369 370 371 372 373 374 375 376
    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;
Per Cederqvist's avatar
Per Cederqvist committed
377 378
}

379 380
void
init_person  (Person *p)
Per Cederqvist's avatar
Per Cederqvist committed
381
{
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    p->user_area = 0;
    p->total_time_present = 0;
    p->sessions = 0;
    p->created_lines = 0;
    p->created_bytes = 0;
    p->read_texts = 0;
    p->no_of_text_fetches = 0;
    p->created_persons = 0;
    p->created_confs = 0;
    p->username = EMPTY_STRING;
    p->last_login = NO_TIME;

    init_priv_bits(&p->privileges);
    init_personal_flags(&p->flags);
    init_text_list(&p->created_texts);
    init_mark_list(&p->marks);
    init_membership_list(&p->conferences);

    /* No initialization of pwd is done (or needed) */
Per Cederqvist's avatar
Per Cederqvist committed
401 402
}

403
/* Personal_flags */
Per Cederqvist's avatar
Per Cederqvist committed
404

405 406 407 408 409 410 411 412 413 414 415
void
init_personal_flags (Personal_flags *p)
{
    p->unread_is_secret = 0;
    p->flg2 = 0;
    p->flg3 = 0;
    p->flg4 = 0;
    p->flg5 = 0;
    p->flg6 = 0;
    p->flg7 = 0;
    p->flg8 = 0;
Per Cederqvist's avatar
Per Cederqvist committed
416 417
}

418 419 420 421
/* Priv_bits */

void
init_priv_bits (Priv_bits *pb)
Per Cederqvist's avatar
Per Cederqvist committed
422
{
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    pb->wheel = 0;
    pb->admin = 0;
    pb->statistic = 0;
    pb->create_pers = 0;
    pb->create_conf = 0;
    pb->change_name = 0;
    pb->extern_gw = 0;
    pb->flg8 = 0;
    pb->flg9 = 0;
    pb->flg10 = 0;
    pb->flg11 = 0;
    pb->flg12 = 0;
    pb->flg13 = 0;
    pb->flg14 = 0;
    pb->flg15 = 0;
    pb->flg16 = 0;
}
Per Cederqvist's avatar
Per Cederqvist committed
440

441
/* Session_info */
Per Cederqvist's avatar
Per Cederqvist committed
442

443 444 445 446 447 448 449 450 451 452 453
void
init_session_info (Session_info *s)
{
    s->person = 0;
    s->what_am_i_doing = EMPTY_STRING;
    s->username = EMPTY_STRING;
    s->working_conference = 0;
    s->session = 0;
    s->connection_time = NO_TIME;
    s->idle_time = 0;
}
Per Cederqvist's avatar
Per Cederqvist committed
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468
/* Session_info_ident */

void
init_session_info_ident (Session_info_ident *s)
{
    s->person = 0;
    s->what_am_i_doing = EMPTY_STRING;
    s->username = EMPTY_STRING;
    s->ident_user = EMPTY_STRING;
    s->hostname = EMPTY_STRING;
    s->working_conference = 0;
    s->session = 0;
    s->connection_time = NO_TIME;
    s->idle_time = 0;
Per Cederqvist's avatar
Per Cederqvist committed
469 470
}

471 472 473 474
/* Text_list */

static  void
clear_text_list(Text_list *text_list)
Per Cederqvist's avatar
Per Cederqvist committed
475
{
476 477 478
    if ( text_list == NULL )
    {
	log("clear_text_list(): text_list == NULL.\n"); 
Per Cederqvist's avatar
Per Cederqvist committed
479
	return;
480
    }
Per Cederqvist's avatar
Per Cederqvist committed
481

482 483
    sfree(text_list->texts);
    init_text_list(text_list);
Per Cederqvist's avatar
Per Cederqvist committed
484 485
}

486
static  Text_list
Per Cederqvist's avatar
Per Cederqvist committed
487 488 489 490 491 492 493 494 495 496 497
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;
}

498 499
void
init_text_list (Text_list *tl)
Per Cederqvist's avatar
Per Cederqvist committed
500
{
501 502 503
    tl->first_local_no = 1;	/* sic */
    tl->no_of_texts = 0;
    tl->texts = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
504 505
}

506
/* Text_stat */
507

508 509
Text_stat *
alloc_text_stat(void)
Per Cederqvist's avatar
Per Cederqvist committed
510
{
511
    Text_stat *t;
Per Cederqvist's avatar
Per Cederqvist committed
512

513 514 515 516 517
    text_stat_cnt++;
    
    t = smalloc(sizeof(Text_stat));
    init_text_stat(t);
    return t;
Per Cederqvist's avatar
Per Cederqvist committed
518 519
}

520 521 522 523 524
void
free_text_stat(Text_stat *t)
{
    if ( t == NULL )
	return;
525

526 527 528 529 530 531 532
    text_stat_cnt--;
    clear_text_stat(t);
    sfree(t);
}

void
clear_text_stat(Text_stat *t)
Per Cederqvist's avatar
Per Cederqvist committed
533 534 535
{
    int i;

536
    for ( i = 0; i < t->no_of_misc; i++ )
Per Cederqvist's avatar
Per Cederqvist committed
537
    {
538 539 540 541
	switch ( t->misc_items[ i ].type )
	{
	case recpt:
	case cc_recpt:
542
	case bcc_recpt:
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	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("%s: free_text_stat: unknown Info_type %d.",
			__FILE__, t->misc_items[ i ].type );
	}
Per Cederqvist's avatar
Per Cederqvist committed
558 559
    }

560
    sfree(t->misc_items);
David Byers's avatar
David Byers committed
561
    free_aux_item_list(&t->aux_item_list);
562
    init_text_stat(t);
Per Cederqvist's avatar
Per Cederqvist committed
563 564
}

565 566
Text_stat *
copy_text_stat(Text_stat *t)
567
{
568
    Text_stat *c;
569

570 571 572 573
    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));
David Byers's avatar
David Byers committed
574 575
    copy_aux_item_list(&c->aux_item_list,
                       &t->aux_item_list);
576 577 578
    return c;
}

579 580
void
init_text_stat  (Text_stat *t)
Per Cederqvist's avatar
Per Cederqvist committed
581
{
582 583 584 585 586 587 588
    t->creation_time = NO_TIME;
    t->file_pos = 0;
    t->author = 0;
    t->no_of_lines = 0;
    t->no_of_chars = 0;
    t->no_of_marks = 0;
    t->no_of_misc = 0;
589
    t->highest_aux = 0;
590
    t->misc_items = NULL;
David Byers's avatar
David Byers committed
591
    init_aux_item_list(&t->aux_item_list);
Per Cederqvist's avatar
Per Cederqvist committed
592 593
}

594
/* struct tm */
595

596 597 598 599 600 601 602 603 604 605 606 607
void
init_struct_tm (struct tm *t)
{
    t->tm_sec = 0;
    t->tm_min = 0;
    t->tm_hour = 0;
    t->tm_mday = 0;
    t->tm_mon = 0;
    t->tm_year = 0;
    t->tm_wday = 0;
    t->tm_yday = 0;
    t->tm_isdst = 0;
608 609
}

610 611 612 613
/* Who_info */

void
init_who_info (Who_info *w)
614
{
615 616 617 618 619 620
    w->person = 0;
    w->what_am_i_doing = EMPTY_STRING;
    w->username = EMPTY_STRING;
    w->working_conference = 0;
    w->session_no = 0;
}
621

622 623 624 625 626 627 628 629 630 631 632 633
/* Who_info_ident */

void
init_who_info_ident (Who_info_ident *w)
{
    w->person = 0;
    w->what_am_i_doing = EMPTY_STRING;
    w->username = EMPTY_STRING;
    w->ident_user = EMPTY_STRING;
    w->hostname = EMPTY_STRING;
    w->working_conference = 0;
    w->session_no = 0;
634 635
}

636
/* Who_info_old */
637

638 639 640 641 642 643 644 645
void
init_who_info_old (Who_info_old *w)
{
    w->person = 0;
    w->what_am_i_doing = EMPTY_STRING;
    w->working_conference = 0;
}

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
/* Aux_item_list */

void
free_aux_item_list(Aux_item_list *list)
{
    unsigned long i;

    if (list->items != NULL)
    {
        for (i = 0; i < list->length; i++)
            s_clear(&list->items[i].data);
    }
    list->length = 0;
    sfree(list->items);
    list->items = NULL;
}

David Byers's avatar
David Byers committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
void
copy_aux_item_list(Aux_item_list *dest, Aux_item_list *src)
{
    unsigned long   i;

    *dest = *src;
    dest->items = smalloc(sizeof(Aux_item) * src->length);
    for (i = 0; i < src->length; i++)
    {
        copy_aux_item(&dest->items[i], &src->items[i]);
    }
}

void
init_aux_item_list(Aux_item_list *list)
{
    list->length = 0;
    list->items = NULL;
}

void
copy_aux_item(Aux_item *dest, Aux_item *src)
{
    *dest = *src;
    dest->data = EMPTY_STRING;
    s_strcpy(&dest->data, src->data);
}


692 693 694 695
/*
 * Other kind of functions 
 */
void
Per Cederqvist's avatar
Per Cederqvist committed
696 697
dump_alloc_counts(FILE *fp)
{
Per Cederqvist's avatar
Per Cederqvist committed
698 699 700
    fprintf(fp, "---memory.c:\n\tperson:          %d\n\tconference:      %d\n",
	    person_cnt, conference_cnt);
    fprintf(fp, "\ttext_stat:       %d\n", text_stat_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
701
}