Skip to content
Snippets Groups Projects
Select Git revision
  • 1e4a64d9dc7e7098bd42c643ac4eeab3d1f8e471
  • master default protected
  • 9.0
  • marcus/wix3
  • 8.0
  • nt-tools
  • 7.8
  • 7.6
  • 7.4
  • 7.2
  • 7.0
  • 0.6
  • rosuav/latex-markdown-renderer
  • rxnpatch/rxnpatch
  • marcus/gobject-introspection
  • rxnpatch/8.0
  • rosuav/pre-listening-ports
  • rosuav/async-annotations
  • rosuav/pgsql-ssl
  • rxnpatch/rxnpatch-broken/2023-10-06T094250
  • grubba/fdlib
  • v8.0.2020
  • v8.0.2018
  • v8.0.2016
  • v8.0.2014
  • v8.0.2012
  • v8.0.2008
  • v8.0.2006
  • v8.0.2004
  • v8.0.2002
  • v8.0.2000
  • v8.0.1998
  • v8.0.1996
  • v8.0.1994
  • v8.0.1992
  • v8.0.1990
  • v8.0.1988
  • v8.0.1986
  • rxnpatch/clusters/8.0/2025-04-29T124414
  • rxnpatch/2025-04-29T124414
  • v8.0.1984
41 results

threads.c

Blame
  • memory.c 12.18 KiB
    /*
     * $Id: memory.c,v 0.18 1996/08/02 19:59:34 ceder Exp $
     * Copyright (C) 1991, 1992, 1993, 1994  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/initializers.
     *
     * These functions should be used instead of smalloc/srealloc.
     */
    
    static char *rcsid = "$Id: memory.c,v 0.18 1996/08/02 19:59:34 ceder Exp $";
    #include "rcs.h"
    USE(rcsid);
    
    #include <stdio.h>
    #ifdef HAVE_STRING_H
    #  include <string.h>
    #endif
    #ifdef HAVE_STDARG_H
    #  include <stdarg.h>
    #endif
    #include <sys/types.h>
    #include <time.h>
    
    #include "s-string.h"
    #include "kom-types.h"
    #include "kom-memory.h"
    #include "server/smalloc.h"
    #include "log.h"
    #include "lyskomd.h"
    
    static int person_cnt = 0;
    static int conference_cnt = 0;
    static int text_stat_cnt = 0;
    
    /* Forward declarations */
    
    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);
    
    
    /* Conf_type */
    
    void
    init_conf_type(Conf_type *ct)
    {
        ct->rd_prot = 0;
        ct->original = 0;
        ct->secret = 0;
        ct->letter_box = 0;
    }
    
    /* Conference */
    
    Conference *
    alloc_conference(void)
    {
        Conference *c;
    
        conference_cnt++;
        
        c = smalloc(sizeof(Conference));
        init_conference(c);
        return c;
    }
    
    void
    free_conference(Conference *confp)
    {
        if ( confp == NULL )
    	return;
    
        conference_cnt--;
        clear_conference(confp);
        sfree(confp);
    }
    
    void
    clear_conference(Conference *confp)
    {
        s_clear(&confp->name);
        clear_member_list(&confp->members);
        clear_text_list(&confp->texts);
        init_conference(confp);
    }
    
    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);
        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);
    }
    
    /* 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;
        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;
        d->what_am_i_doing = EMPTY_STRING;
    }
    
    
    /* Mark_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);
        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;
    }
    
    void
    init_member_list (Member_list *ml)
    {
        ml->no_of_members = 0;
        ml->members = NULL;
    }
    
    /* Membership */
    
    static  void
    clear_membership(Membership *mship)
    {
        if ( mship == NULL )
        {
    	log("clear_membership(): mship == NULL.\n");
    	return;
        }
    
        sfree(mship->read_texts);
        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;
    }
    
    /* Membership_list */
    
    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);
        init_membership_list(mlist);
    }
    
    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;
    }
    
    void
    init_membership_list (Membership_list *m)
    {
        m->no_of_confs = 0;
        m->confs = NULL;
    }
    
    /* 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;
    }
    
    /* Person */
    Person *
    alloc_person(void)
    {
        Person *p;
    
        person_cnt++;
        
        p = smalloc(sizeof(Person));
        init_person(p);
        return p;
    }
    
    void
    free_person(Person *person)
    {
        if ( person == NULL )
    	return;
    
        person_cnt--;
        clear_person(person);
        sfree(person);
    }
    
    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);
    }
    
    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;
    }
    
    void
    init_person  (Person *p)
    {
        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) */
    }
    
    /* Personal_flags */
    
    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;
    }
    
    /* Priv_bits */
    
    void
    init_priv_bits (Priv_bits *pb)
    {
        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;
    }
    
    /* Session_info */
    
    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;
    }
    
    /* 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;
    }
    
    /* Text_list */
    
    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);
        init_text_list(text_list);
    }
    
    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;
    }
    
    void
    init_text_list (Text_list *tl)
    {
        tl->first_local_no = 1;	/* sic */
        tl->no_of_texts = 0;
        tl->texts = NULL;
    }
    
    /* Text_stat */
    
    Text_stat *
    alloc_text_stat(void)
    {
        Text_stat *t;
    
        text_stat_cnt++;
        
        t = smalloc(sizeof(Text_stat));
        init_text_stat(t);
        return t;
    }
    
    void
    free_text_stat(Text_stat *t)
    {
        if ( t == NULL )
    	return;
    
        text_stat_cnt--;
        clear_text_stat(t);
        sfree(t);
    }
    
    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("%s: free_text_stat: unknown Info_type %d.",
    			__FILE__, t->misc_items[ i ].type );
    	}
        }
    
        sfree(t->misc_items);
        init_text_stat(t);
    }
    
    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;
    }
    
    void
    init_text_stat  (Text_stat *t)
    {
        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;
        t->misc_items = NULL;
    }
    
    /* struct tm */
    
    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;
    }
    
    /* Who_info */
    
    void
    init_who_info (Who_info *w)
    {
        w->person = 0;
        w->what_am_i_doing = EMPTY_STRING;
        w->username = EMPTY_STRING;
        w->working_conference = 0;
        w->session_no = 0;
    }
    
    /* 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;
    }
    
    /* Who_info_old */
    
    void
    init_who_info_old (Who_info_old *w)
    {
        w->person = 0;
        w->what_am_i_doing = EMPTY_STRING;
        w->working_conference = 0;
    }
    
    /*
     * Other kind of functions 
     */
    void
    dump_alloc_counts(FILE *fp)
    {
        fprintf(fp, "---memory.c:\n\tperson:          %d\n\tconference:      %d\n",
    	    person_cnt, conference_cnt);
        fprintf(fp, "\ttext_stat:       %d\n", text_stat_cnt);
    }