Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/exelib/common/strbuf.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 1991, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#include <string.h>
24
#include "libhlp.h"
25
#include "exelib_internal.h"
26
27
#define MIN_GROWTH 128
28
29
J9StringBuffer* strBufferCat(struct J9PortLibrary *portLibrary, J9StringBuffer* buffer, const char* string) {
30
UDATA len = strlen(string);
31
32
buffer = strBufferEnsure(portLibrary, buffer, len);
33
if (buffer) {
34
strcat(buffer->data, string);
35
buffer->remaining -= len;
36
}
37
38
return buffer;
39
}
40
41
42
J9StringBuffer* strBufferEnsure(struct J9PortLibrary *portLibrary, J9StringBuffer* buffer, UDATA len) {
43
44
if (buffer == NULL) {
45
PORT_ACCESS_FROM_PORT(portLibrary);
46
UDATA newSize = len > MIN_GROWTH ? len : MIN_GROWTH;
47
buffer = j9mem_allocate_memory( newSize + 1 + sizeof(UDATA), OMRMEM_CATEGORY_VM); /* 1 for null terminator */
48
if (buffer != NULL) {
49
buffer->remaining = newSize;
50
buffer->data[0] = '\0';
51
}
52
return buffer;
53
}
54
55
if (len > buffer->remaining) {
56
PORT_ACCESS_FROM_PORT(portLibrary);
57
UDATA newSize = len > MIN_GROWTH ? len : MIN_GROWTH;
58
J9StringBuffer* new = j9mem_allocate_memory(strlen((const char*)buffer->data) + newSize + sizeof(UDATA) + 1 , OMRMEM_CATEGORY_VM);
59
if (new) {
60
new->remaining = newSize;
61
strcpy(new->data, (const char *)buffer->data);
62
}
63
j9mem_free_memory(buffer);
64
return new;
65
}
66
67
return buffer;
68
}
69
70
71
J9StringBuffer* strBufferPrepend(struct J9PortLibrary *portLibrary, J9StringBuffer* buffer, char* string) {
72
UDATA len = strlen(string);
73
74
buffer = strBufferEnsure(portLibrary, buffer, len);
75
if (buffer) {
76
memmove(buffer->data + len, buffer->data, strlen((const char*)buffer->data) + 1);
77
strncpy(buffer->data, string, len);
78
buffer->remaining -= len;
79
}
80
81
return buffer;
82
}
83
84
85
char* strBufferData(J9StringBuffer* buffer) {
86
return buffer ? buffer->data : NULL;
87
}
88
89