summaryrefslogtreecommitdiff
path: root/libstore/enc.c
blob: 5838c1727ec05efd15e8f45894cacb7988a65f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Store wire encoding/decoding

   Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.ai.mit.edu>
   This file is part of the GNU Hurd.

   The GNU Hurd 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 2, or (at
   your option) any later version.

   The GNU Hurd 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */

#include <string.h>
#include <sys/mman.h>

#include "store.h"

/* Initialize ENC.  The given vector and sizes will be used for the encoding
   if they are big enough (otherwise new ones will be automatically
   allocated).  */
void
store_enc_init (struct store_enc *enc,
		mach_port_t *ports, mach_msg_type_number_t num_ports,
		int *ints, mach_msg_type_number_t num_ints,
		off_t *offsets, mach_msg_type_number_t num_offsets,
		char *data, mach_msg_type_number_t data_len)
{
  memset (enc, 0, sizeof(*enc));

  enc->ports = enc->init_ports = ports;
  enc->num_ports = num_ports;
  enc->ints = enc->init_ints = ints;
  enc->num_ints = num_ints;
  enc->offsets = enc->init_offsets = offsets;
  enc->num_offsets = num_offsets;
  enc->data = enc->init_data = data;
  enc->data_len = data_len;
}

/* Deallocate storage used by the fields in ENC (but nothing is done with ENC
   itself).  */
void
store_enc_dealloc (struct store_enc *enc)
{
  if (enc->ports && enc->num_ports > 0)
    /* For ports, we must deallocate each port as well.  */
    {
      while (enc->cur_port < enc->num_ports)
	{
	  mach_port_t port = enc->ports[enc->cur_port++];
	  if (port != MACH_PORT_NULL)
	    mach_port_deallocate (mach_task_self (), port);
	}

      if (enc->ports != enc->init_ports)
	munmap ((caddr_t) enc->ports, enc->num_ports * sizeof (*enc->ports));
    }

  if (enc->ints && enc->num_ints > 0 && enc->ints != enc->init_ints)
    munmap ((caddr_t) enc->ints, enc->num_ints * sizeof (*enc->ints));

  if (enc->offsets && enc->num_offsets > 0
      && enc->offsets != enc->init_offsets)
    munmap ((caddr_t) enc->offsets, enc->num_offsets * sizeof (*enc->offsets));

  if (enc->data && enc->data_len > 0 && enc->data != enc->init_data)
    munmap (enc->data, enc->data_len);

  /* For good measure...  */
  memset (enc, 0, sizeof(*enc));
}

/* Copy out the parameters from ENC into the given variables suitably for
   returning from a file_get_storage_info rpc, and deallocate ENC.  */
void
store_enc_return (struct store_enc *enc,
		  mach_port_t **ports, mach_msg_type_number_t *num_ports,
		  int **ints, mach_msg_type_number_t *num_ints,
		  off_t **offsets, mach_msg_type_number_t *num_offsets,
		  char **data, mach_msg_type_number_t *data_len)
{
  *ports = enc->ports;
  *num_ports = enc->num_ports;
  *ints = enc->ints;
  *num_ints = enc->num_ints;
  *offsets = enc->offsets;
  *num_offsets = enc->num_offsets;
  *data = enc->data;
  *data_len = enc->data_len;
}