forked from contiki-os/contiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharg.c
133 lines (126 loc) · 4.15 KB
/
arg.c
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* \file
* Argument buffer for passing arguments when starting processes
* \author Adam Dunkels <[email protected]>
*/
/**
* \addtogroup sys
* @{
*/
/**
* \defgroup arg Argument buffer
* @{
*
* The argument buffer can be used when passing an argument from an
* exiting process to a process that has not been created yet. Since
* the exiting process will have exited when the new process is
* started, the argument cannot be passed in any of the processes'
* addres spaces. In such situations, the argument buffer can be used.
*
* The argument buffer is statically allocated in memory and is
* globally accessible to all processes.
*
* An argument buffer is allocated with the arg_alloc() function and
* deallocated with the arg_free() function. The arg_free() function
* is designed so that it can take any pointer, not just an argument
* buffer pointer. If the pointer to arg_free() is not an argument
* buffer, the function does nothing.
*/
/*
* Copyright (c) 2003, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the Contiki desktop OS
*
*
*/
#include "contiki.h"
#include "sys/arg.h"
/**
* \internal Structure used for holding an argument buffer.
*/
struct argbuf {
char buf[128];
char used;
};
static struct argbuf bufs[1];
/*-----------------------------------------------------------------------------------*/
/**
* \internal Initalizer, called by the dispatcher module.
*/
/*-----------------------------------------------------------------------------------*/
void
arg_init(void)
{
bufs[0].used = 0;
}
/*-----------------------------------------------------------------------------------*/
/**
* Allocates an argument buffer.
*
* \param size The requested size of the buffer, in bytes.
*
* \return Pointer to allocated buffer, or NULL if no buffer could be
* allocated.
*
* \note It currently is not possible to allocate argument buffers of
* any other size than 128 bytes.
*
*/
/*-----------------------------------------------------------------------------------*/
char *
arg_alloc(char size)
{
if(bufs[0].used == 0) {
bufs[0].used = 1;
return bufs[0].buf;
}
return 0;
}
/*-----------------------------------------------------------------------------------*/
/**
* Deallocates an argument buffer.
*
* This function deallocates the argument buffer pointed to by the
* parameter, but only if the buffer actually is an argument buffer
* and is allocated. It is perfectly safe to call this function with
* any pointer.
*
* \param arg A pointer.
*/
/*-----------------------------------------------------------------------------------*/
void
arg_free(char *arg)
{
if(arg == bufs[0].buf) {
bufs[0].used = 0;
}
}
/*-----------------------------------------------------------------------------------*/
/** @} */
/** @} */