aboutsummaryrefslogtreecommitdiffstats
path: root/tmk_core/common/sendchar.h
blob: 7a64d00c7f7aade309906eb737f411c5a227835b (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
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

This program 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 of the License, or
(at your option) any later version.

This program 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, see <http://www.gnu.org/licenses/>.
*/

#ifndef SENDCHAR_H
#define SENDCHAR_H

#include <stdint.h>


#ifdef __cplusplus
extern "C" {
#endif

/* transmit a character.  return 0 on success, -1 on error. */
int8_t sendchar(uint8_t c);

#ifdef __cplusplus
}
#endif

#endif
highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Copyright (C) 2009      Citrix Ltd.
 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program 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 Lesser General Public License for more details.
 */

#include "libxl_internal.h"
#include <stdarg.h>

/*
 * It is safe to store gc in the struct because:
 * - If it an actual gc, then the flexarray should not be used after the gc
 *   have been freed.
 * - If it is a NOGC, then this point to a structure embedded in libxl_ctx,
 *   therefore will survive across several libxl calls.
 */

flexarray_t *flexarray_make(libxl__gc *gc, int size, int autogrow)
{
    flexarray_t *array;

    GCNEW(array);
    array->size = size;
    array->autogrow = autogrow;
    array->count = 0;
    array->gc = gc;
    GCNEW_ARRAY(array->data, size);

    return array;
}

void flexarray_free(flexarray_t *array)
{
    assert(!libxl__gc_is_real(array->gc));
    free(array->data);
    free(array);
}

void flexarray_grow(flexarray_t *array, int extents)
{
    int newsize;
    libxl__gc *gc = array->gc;

    newsize = array->size + extents;
    GCREALLOC_ARRAY(array->data, newsize);
    array->size += extents;
}

int flexarray_set(flexarray_t *array, unsigned int idx, void *ptr)
{
    if (idx >= array->size) {
        int newsize;
        if (!array->autogrow)
            return 1;
        newsize = (array->size * 2 < idx) ? idx + 1 : array->size * 2;
        flexarray_grow(array, newsize - array->size);
    }
    if ( idx + 1 > array->count )
        array->count = idx + 1;
    array->data[idx] = ptr;
    return 0;
}

int flexarray_append(flexarray_t *array, void *ptr)
{
    return flexarray_set(array, array->count, ptr);
}

int flexarray_append_pair(flexarray_t *array, void *ptr1, void *ptr2)
{
    int rc = flexarray_append(array, ptr1);
    if (!rc)
        rc = flexarray_append(array, ptr2);
    return rc;
}

int flexarray_vappend(flexarray_t *array, ...)
{
    va_list va;
    void *ptr;
    int ret;

    va_start(va, array);
    for(ret = 0; (ptr = va_arg(va, void *)); ret++) {
        if ( flexarray_append(array, ptr) )
            break;
    }
    va_end(va);
    return ret;
}

int flexarray_get(flexarray_t *array, int idx, void **ptr)
{
    if (idx >= array->size)
        return 1;
    *ptr = array->data[idx];
    return 0;
}

void **flexarray_contents(flexarray_t *array)
{
    void **data;
    data = array->data;
    if (!libxl__gc_is_real(array->gc))
        free(array);
    return data;
}

/*
 * Local variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */