summaryrefslogtreecommitdiff
path: root/src/libaudcore/stringbuf.cc
blob: 041b1e9de79bfb2245aaa7cfa8a4a5d3d0cf5f23 (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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * stringbuf.cc
 * Copyright 2014 John Lindgren
 *
 * 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
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <pthread.h>
#include <stdlib.h>
#include <string.h>

#include <new>

#include "objects.h"

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/mman.h>
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
#endif

struct StringStack
{
    static constexpr int Size = 1048576;  // 1 MB

    char * top;
    char buf[Size - sizeof top];
};

// adds one byte for null character and rounds up to word boundary
static constexpr int align (int len)
{
    return (len + sizeof (void *)) & ~(sizeof (void *) - 1);
}

static pthread_key_t key;
static pthread_once_t once = PTHREAD_ONCE_INIT;

#ifdef _WIN32
static HANDLE mapping;
#endif

static void free_stack (void * stack)
{
    if (stack)
#ifdef _WIN32
        UnmapViewOfFile (stack);
#else
        munmap (stack, sizeof (StringStack));
#endif
}

static void make_key ()
{
    pthread_key_create (& key, free_stack);

#ifdef _WIN32
    mapping = CreateFileMappingW (INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE,
     0, sizeof (StringStack), nullptr);

    if (! mapping)
        throw std::bad_alloc ();
#endif
}

static StringStack * get_stack ()
{
    pthread_once (& once, make_key);

    StringStack * stack = (StringStack *) pthread_getspecific (key);

    if (! stack)
    {
#ifdef _WIN32
        stack = (StringStack *) MapViewOfFile (mapping, FILE_MAP_COPY, 0, 0, sizeof (StringStack));

        if (! stack)
            throw std::bad_alloc ();
#else
        stack = (StringStack *) mmap (nullptr, sizeof (StringStack),
         PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

        if (stack == MAP_FAILED)
            throw std::bad_alloc ();
#endif

        stack->top = stack->buf;
        pthread_setspecific (key, stack);
    }

    return stack;
}

EXPORT void StringBuf::resize (int len)
{
    if (! stack)
    {
        stack = get_stack ();
        m_data = stack->top;
    }
    else
    {
        if (m_data + align (m_len) != stack->top)
            throw std::bad_alloc ();
    }

    if (len < 0)
    {
        stack->top = stack->buf + sizeof stack->buf;
        m_len = stack->top - m_data - 1;

        if (m_len < 0)
            throw std::bad_alloc ();
    }
    else
    {
        stack->top = m_data + align (len);

        if (stack->top - stack->buf > (int) sizeof stack->buf)
            throw std::bad_alloc ();

        m_data[len] = 0;
        m_len = len;
    }
}

EXPORT StringBuf::~StringBuf ()
{
    if (m_data)
    {
        if (m_data + align (m_len) != stack->top)
            throw std::bad_alloc ();

        stack->top = m_data;
    }
}

EXPORT void StringBuf::steal (StringBuf && other)
{
    if (other.m_data)
    {
        if (m_data)
        {
            if (m_data + align (m_len) != other.m_data ||
             other.m_data + align (other.m_len) != stack->top)
                throw std::bad_alloc ();

            m_len = other.m_len;
            memmove (m_data, other.m_data, m_len + 1);
            stack->top = m_data + align (m_len);
        }
        else
        {
            stack = other.stack;
            m_data = other.m_data;
            m_len = other.m_len;
        }

        other.stack = nullptr;
        other.m_data = nullptr;
        other.m_len = 0;
    }
    else
    {
        if (m_data)
        {
            this->~StringBuf ();
            stack = nullptr;
            m_data = nullptr;
            m_len = 0;
        }
    }
}

EXPORT void StringBuf::combine (StringBuf && other)
{
    if (! other.m_data)
        return;

    if (m_data)
    {
        if (m_data + align (m_len) != other.m_data ||
         other.m_data + align (other.m_len) != stack->top)
            throw std::bad_alloc ();

        memmove (m_data + m_len, other.m_data, other.m_len + 1);
        m_len += other.m_len;
        stack->top = m_data + align (m_len);
    }
    else
    {
        stack = other.stack;
        m_data = other.m_data;
        m_len = other.m_len;
    }

    other.stack = nullptr;
    other.m_data = nullptr;
    other.m_len = 0;
}

EXPORT void StringBuf::insert (int pos, const char * s, int len)
{
    int len0 = m_len;

    if (pos < 0)
        pos = len0;
    if (len < 0)
        len = strlen (s);

    resize (len0 + len);
    memmove (m_data + pos + len, m_data + pos, len0 - pos);
    memcpy (m_data + pos, s, len);
}

EXPORT void StringBuf::remove (int pos, int len)
{
    int after = m_len - pos - len;
    memmove (m_data + pos, m_data + pos + len, after);
    resize (pos + after);
}