Annotation of Gnu-Mach/ipc/ipc_table.c, revision 1.1.1.3

1.1       root        1: /* 
                      2:  * Mach Operating System
                      3:  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
                      4:  * All Rights Reserved.
                      5:  * 
                      6:  * Permission to use, copy, modify and distribute this software and its
                      7:  * documentation is hereby granted, provided that both the copyright
                      8:  * notice and this permission notice appear in all copies of the
                      9:  * software, derivative works or modified versions, and any portions
                     10:  * thereof, and that both notices appear in supporting documentation.
                     11:  * 
                     12:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     13:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
                     14:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
                     15:  * 
                     16:  * Carnegie Mellon requests users of this software to return to
                     17:  * 
                     18:  *  Software Distribution Coordinator  or  [email protected]
                     19:  *  School of Computer Science
                     20:  *  Carnegie Mellon University
                     21:  *  Pittsburgh PA 15213-3890
                     22:  * 
                     23:  * any improvements or extensions that they make and grant Carnegie Mellon
                     24:  * the rights to redistribute these changes.
                     25:  */
                     26: /*
                     27:  */
                     28: /*
                     29:  *     File:   ipc/ipc_table.c
                     30:  *     Author: Rich Draves
                     31:  *     Date:   1989
                     32:  *
                     33:  *     Functions to manipulate tables of IPC capabilities.
                     34:  */
                     35: 
                     36: #include <mach/kern_return.h>
                     37: #include <mach/vm_param.h>
                     38: #include <ipc/ipc_table.h>
                     39: #include <ipc/ipc_port.h>
                     40: #include <ipc/ipc_entry.h>
                     41: #include <kern/kalloc.h>
1.1.1.2   root       42: #include <kern/slab.h>
1.1       root       43: #include <vm/vm_kern.h>
                     44: 
                     45: ipc_table_size_t ipc_table_entries;
1.1.1.3 ! root       46: const unsigned int ipc_table_entries_size = 512;
1.1       root       47: 
                     48: ipc_table_size_t ipc_table_dnrequests;
1.1.1.3 ! root       49: const unsigned int ipc_table_dnrequests_size = 64;
1.1       root       50: 
                     51: void
                     52: ipc_table_fill(
                     53:        ipc_table_size_t        its,         /* array to fill */
                     54:        unsigned int            num,         /* size of array */
                     55:        unsigned int            min,         /* at least this many elements */
                     56:        vm_size_t               elemsize)    /* size of elements */
                     57: {
                     58:        unsigned int index;
                     59:        vm_size_t minsize = min * elemsize;
                     60:        vm_size_t size;
                     61:        vm_size_t incrsize;
                     62: 
                     63:        /* first use powers of two, up to the page size */
                     64: 
                     65:        for (index = 0, size = 1;
                     66:             (index < num) && (size < PAGE_SIZE);
                     67:             size <<= 1) {
                     68:                if (size >= minsize) {
                     69:                        its[index].its_size = size / elemsize;
                     70:                        index++;
                     71:                }
                     72:        }
                     73: 
                     74:        /* then increments of a page, then two pages, etc. */
                     75: 
                     76:        for (incrsize = PAGE_SIZE; index < num;) {
                     77:                unsigned int period;
                     78: 
                     79:                for (period = 0;
                     80:                     (period < 15) && (index < num);
                     81:                     period++, size += incrsize) {
                     82:                        if (size >= minsize) {
                     83:                                its[index].its_size = size / elemsize;
                     84:                                index++;
                     85:                        }
                     86:                }
                     87:                if (incrsize < (PAGE_SIZE << 3))
                     88:                        incrsize <<= 1;
                     89:        }
                     90: }
                     91: 
                     92: void
                     93: ipc_table_init(void)
                     94: {
                     95:        ipc_table_entries = (ipc_table_size_t)
                     96:                kalloc(sizeof(struct ipc_table_size) *
                     97:                       ipc_table_entries_size);
                     98:        assert(ipc_table_entries != ITS_NULL);
                     99: 
                    100:        ipc_table_fill(ipc_table_entries, ipc_table_entries_size - 1,
                    101:                       4, sizeof(struct ipc_entry));
                    102: 
                    103:        /* the last two elements should have the same size */
                    104: 
                    105:        ipc_table_entries[ipc_table_entries_size - 1].its_size =
                    106:                ipc_table_entries[ipc_table_entries_size - 2].its_size;
                    107: 
                    108: 
                    109:        ipc_table_dnrequests = (ipc_table_size_t)
                    110:                kalloc(sizeof(struct ipc_table_size) *
                    111:                       ipc_table_dnrequests_size);
                    112:        assert(ipc_table_dnrequests != ITS_NULL);
                    113: 
                    114:        ipc_table_fill(ipc_table_dnrequests, ipc_table_dnrequests_size - 1,
                    115:                       2, sizeof(struct ipc_port_request));
                    116: 
                    117:        /* the last element should have zero size */
                    118: 
                    119:        ipc_table_dnrequests[ipc_table_dnrequests_size - 1].its_size = 0;
                    120: }
                    121: 
                    122: /*
                    123:  *     Routine:        ipc_table_alloc
                    124:  *     Purpose:
                    125:  *             Allocate a table.
                    126:  *     Conditions:
                    127:  *             May block.
                    128:  */
                    129: 
                    130: vm_offset_t
                    131: ipc_table_alloc(
                    132:        vm_size_t       size)
                    133: {
                    134:        vm_offset_t table;
                    135: 
                    136:        if (size < PAGE_SIZE)
                    137:                table = kalloc(size);
                    138:        else
1.1.1.2   root      139:                if (kmem_alloc(kmem_map, &table, size) != KERN_SUCCESS)
1.1       root      140:                        table = 0;
                    141: 
                    142:        return table;
                    143: }
                    144: 
                    145: /*
                    146:  *     Routine:        ipc_table_realloc
                    147:  *     Purpose:
                    148:  *             Reallocate a big table.
                    149:  *
                    150:  *             The new table remaps the old table,
                    151:  *             so copying is not necessary.
                    152:  *     Conditions:
                    153:  *             Only works for page-size or bigger tables.
                    154:  *             May block.
                    155:  */
                    156: 
                    157: vm_offset_t
                    158: ipc_table_realloc(
                    159:        vm_size_t       old_size,
                    160:        vm_offset_t     old_table,
                    161:        vm_size_t       new_size)
                    162: {
                    163:        vm_offset_t new_table;
                    164: 
1.1.1.2   root      165:        if (kmem_realloc(kmem_map, old_table, old_size,
1.1       root      166:                         &new_table, new_size) != KERN_SUCCESS)
                    167:                new_table = 0;
                    168: 
                    169:        return new_table;
                    170: }
                    171: 
                    172: /*
                    173:  *     Routine:        ipc_table_free
                    174:  *     Purpose:
                    175:  *             Free a table allocated with ipc_table_alloc or
                    176:  *             ipc_table_realloc.
                    177:  *     Conditions:
                    178:  *             May block.
                    179:  */
                    180: 
                    181: void
                    182: ipc_table_free(
                    183:        vm_size_t       size,
                    184:        vm_offset_t     table)
                    185: {
                    186:        if (size < PAGE_SIZE)
                    187:                kfree(table, size);
                    188:        else
1.1.1.2   root      189:                kmem_free(kmem_map, table, size);
1.1       root      190: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.