| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "2api.h" |
| 9 |
#include "2common.h" |
| 10 |
#include "2misc.h" |
| 11 |
#include "2sysincludes.h" |
| 12 |
|
| 13 |
void vb2_workbuf_from_ctx(struct vb2_context *ctx, struct vb2_workbuf *wb) |
| 14 |
{ |
| 15 |
struct vb2_shared_data *sd = vb2_get_sd(ctx); |
| 16 |
vb2_workbuf_init(wb, (void *)sd + sd->workbuf_used, |
| 17 |
sd->workbuf_size - sd->workbuf_used); |
| 18 |
} |
| 19 |
|
| 20 |
void vb2_set_workbuf_used(struct vb2_context *ctx, uint32_t used) |
| 21 |
{ |
| 22 |
struct vb2_shared_data *sd = vb2_get_sd(ctx); |
| 23 |
sd->workbuf_used = vb2_wb_round_up(used); |
| 24 |
} |
| 25 |
|
| 26 |
vb2_error_t vb2api_init(void *workbuf, uint32_t size, |
| 27 |
struct vb2_context **ctxptr) |
| 28 |
{ |
| 29 |
struct vb2_shared_data *sd = workbuf; |
| 30 |
*ctxptr = NULL; |
| 31 |
|
| 32 |
if (!vb2_aligned(workbuf, VB2_WORKBUF_ALIGN)) |
| 33 |
return VB2_ERROR_WORKBUF_ALIGN; |
| 34 |
|
| 35 |
if (size < vb2_wb_round_up(sizeof(*sd))) |
| 36 |
return VB2_ERROR_WORKBUF_SMALL; |
| 37 |
|
| 38 |
|
| 39 |
memset(sd, 0, sizeof(*sd)); |
| 40 |
|
| 41 |
|
| 42 |
sd->magic = VB2_SHARED_DATA_MAGIC; |
| 43 |
sd->struct_version_major = VB2_SHARED_DATA_VERSION_MAJOR; |
| 44 |
sd->struct_version_minor = VB2_SHARED_DATA_VERSION_MINOR; |
| 45 |
sd->workbuf_size = size; |
| 46 |
sd->workbuf_used = vb2_wb_round_up(sizeof(*sd)); |
| 47 |
|
| 48 |
*ctxptr = &sd->ctx; |
| 49 |
return VB2_SUCCESS; |
| 50 |
} |
| 51 |
|
| 52 |
#pragma GCC diagnostic push |
| 53 |
|
| 54 |
#pragma GCC diagnostic ignored "-Wtype-limits" |
| 55 |
vb2_error_t vb2api_relocate(void *new_workbuf, const void *cur_workbuf, |
| 56 |
uint32_t size, struct vb2_context **ctxptr) |
| 57 |
{ |
| 58 |
const struct vb2_shared_data *cur_sd = cur_workbuf; |
| 59 |
struct vb2_shared_data *new_sd; |
| 60 |
|
| 61 |
if (!vb2_aligned(new_workbuf, VB2_WORKBUF_ALIGN)) |
| 62 |
return VB2_ERROR_WORKBUF_ALIGN; |
| 63 |
|
| 64 |
|
| 65 |
if (cur_sd->magic != VB2_SHARED_DATA_MAGIC) |
| 66 |
return VB2_ERROR_SHARED_DATA_MAGIC; |
| 67 |
|
| 68 |
if (cur_sd->struct_version_major != VB2_SHARED_DATA_VERSION_MAJOR || |
| 69 |
cur_sd->struct_version_minor < VB2_SHARED_DATA_VERSION_MINOR) |
| 70 |
return VB2_ERROR_SHARED_DATA_VERSION; |
| 71 |
|
| 72 |
|
| 73 |
if (cur_sd->workbuf_used < vb2_wb_round_up(sizeof(*cur_sd))) |
| 74 |
return VB2_ERROR_WORKBUF_INVALID; |
| 75 |
|
| 76 |
if (cur_sd->workbuf_size < cur_sd->workbuf_used) |
| 77 |
return VB2_ERROR_WORKBUF_INVALID; |
| 78 |
|
| 79 |
if (cur_sd->workbuf_used > size) |
| 80 |
return VB2_ERROR_WORKBUF_SMALL; |
| 81 |
|
| 82 |
|
| 83 |
if (cur_workbuf != new_workbuf) |
| 84 |
memmove(new_workbuf, cur_workbuf, cur_sd->workbuf_used); |
| 85 |
|
| 86 |
|
| 87 |
new_sd = new_workbuf; |
| 88 |
new_sd->workbuf_size = size; |
| 89 |
*ctxptr = &new_sd->ctx; |
| 90 |
|
| 91 |
return VB2_SUCCESS; |
| 92 |
} |
| 93 |
#pragma GCC diagnostic pop |
| 94 |
|
| 95 |
vb2_error_t vb2api_reinit(void *workbuf, struct vb2_context **ctxptr) |
| 96 |
{ |
| 97 |
|
| 98 |
|
| 99 |
struct vb2_shared_data *sd = workbuf; |
| 100 |
return vb2api_relocate(workbuf, workbuf, sd->workbuf_size, ctxptr); |
| 101 |
} |