adamdunkels | 3529869 | 2003-08-31 22:16:49 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * Argument buffer for passing arguments when starting processes |
| 4 | * \author Adam Dunkels <adam@dunkels.com> |
adamdunkels | e937ded | 2003-10-01 07:53:57 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * \addtogroup kernel |
| 9 | * @{ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * \page arg Argument buffer |
adamdunkels | 3529869 | 2003-08-31 22:16:49 +0000 | [diff] [blame] | 14 | * |
| 15 | * The argument buffer can be used when passing an argument from an |
| 16 | * exiting process to a process that has not been created yet. Since |
| 17 | * the exiting process will have exited when the new process is |
| 18 | * started, the argument cannot be passed in any of the processes' |
| 19 | * addres spaces. In such situations, the argument buffer can be used. |
| 20 | * |
| 21 | * The argument buffer is statically allocated in memory and is |
| 22 | * globally accessible to all processes. |
| 23 | * |
| 24 | * An argument buffer is allocated with the arg_alloc() function and |
| 25 | * deallocated with the arg_free() function. The arg_free() function |
| 26 | * is designed so that it can take any pointer, not just an argument |
| 27 | * buffer pointer. If the pointer to arg_free() is not an argument |
| 28 | * buffer, the function does nothing. |
| 29 | */ |
| 30 | |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Copyright (c) 2003, Adam Dunkels. |
| 33 | * All rights reserved. |
| 34 | * |
| 35 | * Redistribution and use in source and binary forms, with or without |
| 36 | * modification, are permitted provided that the following conditions |
| 37 | * are met: |
| 38 | * 1. Redistributions of source code must retain the above copyright |
| 39 | * notice, this list of conditions and the following disclaimer. |
| 40 | * 2. Redistributions in binary form must reproduce the above |
| 41 | * copyright notice, this list of conditions and the following |
| 42 | * disclaimer in the documentation and/or other materials provided |
| 43 | * with the distribution. |
adamdunkels | 3529869 | 2003-08-31 22:16:49 +0000 | [diff] [blame] | 44 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 45 | * products derived from this software without specific prior |
| 46 | * written permission. |
| 47 | * |
| 48 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 49 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 50 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 52 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 54 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 55 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 56 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 57 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 58 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 59 | * |
| 60 | * This file is part of the Contiki desktop OS |
| 61 | * |
adamdunkels | e937ded | 2003-10-01 07:53:57 +0000 | [diff] [blame] | 62 | * $Id: arg.c,v 1.3 2003/10/01 07:53:57 adamdunkels Exp $ |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 63 | * |
| 64 | */ |
| 65 | |
| 66 | #include "arg.h" |
| 67 | |
adamdunkels | 3529869 | 2003-08-31 22:16:49 +0000 | [diff] [blame] | 68 | /** |
| 69 | * \internal Structure used for holding an argument buffer. |
| 70 | */ |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 71 | struct argbuf { |
| 72 | char buf[128]; |
| 73 | char used; |
| 74 | }; |
| 75 | |
| 76 | static struct argbuf bufs[1]; |
| 77 | |
| 78 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 3529869 | 2003-08-31 22:16:49 +0000 | [diff] [blame] | 79 | /** |
| 80 | * \internal Initalizer, called by the dispatcher module. |
| 81 | */ |
| 82 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 83 | void |
| 84 | arg_init(void) |
| 85 | { |
| 86 | bufs[0].used = 0; |
| 87 | } |
| 88 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 3529869 | 2003-08-31 22:16:49 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Allocates an argument buffer. |
| 91 | * |
| 92 | * \param size The requested size of the buffer, in bytes. |
| 93 | * |
| 94 | * \return Pointer to allocated buffer, or NULL if no buffer could be |
| 95 | * allocated. |
| 96 | * |
| 97 | * \note It currently is not possible to allocate argument buffers of |
| 98 | * any other size than 128 bytes. |
| 99 | * |
| 100 | */ |
| 101 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 102 | char * |
| 103 | arg_alloc(char size) |
| 104 | { |
| 105 | if(bufs[0].used == 0) { |
| 106 | bufs[0].used = 1; |
adamdunkels | e937ded | 2003-10-01 07:53:57 +0000 | [diff] [blame] | 107 | return bufs[0].buf; |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 3529869 | 2003-08-31 22:16:49 +0000 | [diff] [blame] | 112 | /** |
| 113 | * Deallocates an argument buffer. |
| 114 | * |
| 115 | * This function deallocates the argument buffer pointed to by the |
| 116 | * parameter, but only if the buffer actually is an argument buffer |
| 117 | * and is allocated. It is perfectly safe to call this function with |
| 118 | * any pointer. |
| 119 | * |
| 120 | * \param arg A pointer. |
| 121 | */ |
| 122 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 564b903 | 2003-08-27 12:01:46 +0000 | [diff] [blame] | 123 | void |
| 124 | arg_free(char *arg) |
| 125 | { |
| 126 | if(arg == bufs[0].buf) { |
| 127 | bufs[0].used = 0; |
| 128 | } |
| 129 | } |
| 130 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | e937ded | 2003-10-01 07:53:57 +0000 | [diff] [blame] | 131 | /** @} */ |