PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
copy.c
Go to the documentation of this file.
2
3#include <kernel/log/log.h>
8
9#include <errno.h>
10
12{
13 if (src == NULL || dest == NULL)
14 {
15 errno = EINVAL;
16 return ERR;
17 }
18
19 if (!(src->type & AML_DATA_REF_OBJECTS))
20 {
21 LOG_ERR("cannot copy object of type '%s'\n", aml_type_to_string(src->type));
22 errno = EINVAL;
23 return ERR;
24 }
25
26 switch (src->type)
27 {
28 case AML_INTEGER:
29 if (aml_integer_set(dest, src->integer.value) == ERR)
30 {
31 return ERR;
32 }
33 break;
34 case AML_STRING:
35 if (aml_string_set(dest, src->string.content) == ERR)
36 {
37 return ERR;
38 }
39 break;
40 case AML_BUFFER:
41 if (aml_buffer_set(dest, src->buffer.content, src->buffer.length, src->buffer.length) == ERR)
42 {
43 return ERR;
44 }
45 break;
46 case AML_PACKAGE:
47 if (aml_package_set(dest, src->package.length) == ERR)
48 {
49 return ERR;
50 }
51
52 for (uint64_t i = 0; i < src->package.length; i++)
53 {
55 {
56 aml_object_clear(dest);
57 return ERR;
58 }
59 }
60 break;
63 {
64 return ERR;
65 }
66 break;
67 default:
68 LOG_ERR("cannot copy object of type '%s'\n", aml_type_to_string(src->type));
69 errno = EINVAL;
70 return ERR;
71 }
72
73 // To make debugging easier we copy the name of the object to if the dest is not already named.
74 // The copied name would be overwritten if the dest is named later.
75 if (!(dest->flags & AML_OBJECT_NAMED) && (src->flags & AML_OBJECT_NAMED))
76 {
77 dest->name = src->name;
78 }
79
80 // Inherits the `AML_OBJECT_EXCEPTION_ON_USE` flag.
81 if (src->flags & AML_OBJECT_EXCEPTION_ON_USE)
82 {
83 dest->flags |= AML_OBJECT_EXCEPTION_ON_USE;
84 }
85 else
86 {
87 dest->flags &= ~AML_OBJECT_EXCEPTION_ON_USE;
88 }
89
90 return 0;
91}
92
94{
95 if (src == NULL || dest == NULL)
96 {
97 errno = EINVAL;
98 return ERR;
99 }
100
101 if (src->type == AML_UNINITIALIZED)
102 {
103 errno = EINVAL;
104 return ERR;
105 }
106
107 if (src == dest)
108 {
109 return 0;
110 }
111
112 if (dest->type == AML_ARG)
113 {
114 if (dest->arg.value == NULL) // Is uninitialized
115 {
116 aml_object_t* newValue = aml_object_new();
117 if (newValue == NULL)
118 {
119 return ERR;
120 }
121
122 dest->arg.value = newValue; // Transfer ownership
123 return aml_copy_data_and_type(src, dest->arg.value);
124 }
125
126 if (dest->arg.value->type == AML_OBJECT_REFERENCE)
127 {
128 return aml_copy_object(state, src, dest->arg.value->objectReference.target);
129 }
130 else
131 {
132 return aml_copy_data_and_type(src, dest->arg.value);
133 }
134 }
135
136 if (dest->type == AML_LOCAL)
137 {
138 return aml_copy_data_and_type(src, dest->local.value);
139 }
140
141 if (dest->type == AML_FIELD_UNIT)
142 {
143 return aml_field_unit_store(state, &dest->fieldUnit, src);
144 }
145 if (dest->type == AML_BUFFER_FIELD)
146 {
147 return aml_buffer_field_store(&dest->bufferField, src);
148 }
149
150 if (dest->flags & AML_OBJECT_NAMED)
151 {
152 return aml_convert_result(state, src, dest);
153 }
154
155 if (dest->type == AML_UNINITIALIZED)
156 {
157 return aml_copy_data_and_type(src, dest);
158 }
159
160 LOG_ERR("illegal copy operation from type '%s' to type '%s'\n", aml_type_to_string(src->type),
161 aml_type_to_string(dest->type));
162 errno = ENOSYS;
163 return ERR;
164}
#define LOG_ERR(format,...)
Definition log.h:108
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOSYS
Function not implemented.
Definition errno.h:222
#define errno
Error number variable.
Definition errno.h:27
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t aml_buffer_field_store(aml_buffer_field_t *bufferField, aml_object_t *in)
Write a value to a BufferField.
uint64_t aml_convert_result(aml_state_t *state, aml_object_t *result, aml_object_t *target)
Performs a "Implicit Result Object Conversion" acording to the rules in section 19....
Definition convert.c:489
uint64_t aml_copy_data_and_type(aml_object_t *src, aml_object_t *dest)
Copies the data and type from the source object to the destination object, completly overwriting it.
Definition copy.c:11
uint64_t aml_copy_object(aml_state_t *state, aml_object_t *src, aml_object_t *dest)
Copies the data from the source object to the destination object.
Definition copy.c:93
uint64_t aml_field_unit_store(aml_state_t *state, aml_field_unit_t *fieldUnit, aml_object_t *in)
Write a value to a FieldUnit. FieldUnits include Fields, IndexFields and BankFields.
Definition field_unit.c:639
uint64_t aml_object_reference_set(aml_object_t *object, aml_object_t *target)
Set a object as an ObjectReference to the given target object.
Definition object.c:895
uint64_t aml_package_set(aml_object_t *object, uint64_t length)
Set a object as a package with the given number of elements.
Definition object.c:940
aml_object_t * aml_object_new(void)
Allocate a new ACPI object.
Definition object.c:62
uint64_t aml_string_set(aml_object_t *object, const char *str)
Set a object as a string with the given value.
Definition object.c:1068
void aml_object_clear(aml_object_t *object)
Clear the data of a object, setting its type to AML_UNINITIALIZED.
Definition object.c:96
uint64_t aml_integer_set(aml_object_t *object, aml_uint_t value)
Set a object as an integer with the given value and bit width.
Definition object.c:772
uint64_t aml_buffer_set(aml_object_t *object, const uint8_t *buffer, uint64_t bytesToCopy, uint64_t length)
Set a object as a buffer with the given content.
Definition object.c:576
@ AML_BUFFER_FIELD
Definition object.h:62
@ AML_PACKAGE
Definition object.h:81
@ AML_OBJECT_REFERENCE
Definition object.h:79
@ AML_STRING
Definition object.h:85
@ AML_ARG
Not in the spec, used internally to represent method arguments.
Definition object.h:90
@ AML_FIELD_UNIT
Definition object.h:66
@ AML_DATA_REF_OBJECTS
Definition object.h:105
@ AML_LOCAL
Not in the spec, used internally to represent method local variables.
Definition object.h:91
@ AML_INTEGER
Definition object.h:67
@ AML_UNINITIALIZED
Definition object.h:60
@ AML_BUFFER
Definition object.h:61
@ AML_OBJECT_EXCEPTION_ON_USE
Definition object.h:135
@ AML_OBJECT_NAMED
Appears in the namespace tree. Will be set in aml_object_add().
Definition object.h:128
const char * aml_type_to_string(aml_type_t type)
Convert an aml data type to a string.
Definition to_string.c:5
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
aml_object_t * value
The object that was passed as the argument.
Definition object.h:429
uint64_t length
Definition object.h:216
uint8_t * content
Definition object.h:215
aml_uint_t value
Definition object.h:288
aml_object_t * value
The value of the local variable.
Definition object.h:439
aml_object_t * target
Definition object.h:337
ACPI object.
Definition object.h:447
aml_package_t package
Definition object.h:464
aml_field_unit_t fieldUnit
Definition object.h:457
aml_integer_t integer
Definition object.h:458
aml_buffer_t buffer
Definition object.h:453
aml_string_t string
Definition object.h:467
aml_local_t local
Definition object.h:472
aml_object_reference_t objectReference
Definition object.h:462
aml_buffer_field_t bufferField
Definition object.h:454
aml_arg_t arg
Definition object.h:471
aml_object_t ** elements
Definition object.h:361
uint64_t length
Definition object.h:362
AML State.
Definition state.h:25
char * content
Definition object.h:392