forked from systemd/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
io-util: split out "struct iovec" related calls into their own .c/.h …
…files This is preparation for systemd#28891, which adds a bunch more helpers around "struct iovec", at which point this really deserves its own .c/.h file. The idea is that we sooner or later can consider "struct iovec" as an entirely generic mechanism to reference some binary blob, and is the go-to type for this purpose whenever we need one.
- Loading branch information
1 parent
8459615
commit f1b27bd
Showing
55 changed files
with
143 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* SPDX-License-Identifier: LGPL-2.1-or-later */ | ||
|
||
#include "iovec-util.h" | ||
#include "string-util.h" | ||
|
||
|
||
char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) { | ||
char *x; | ||
|
||
assert(iovec); | ||
assert(n_iovec); | ||
|
||
x = strjoin(field, value); | ||
if (x) | ||
iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x); | ||
return x; | ||
} | ||
|
||
char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) { | ||
char *x; | ||
|
||
assert(iovec); | ||
assert(n_iovec); | ||
|
||
x = set_iovec_string_field(iovec, n_iovec, field, value); | ||
free(value); | ||
return x; | ||
} | ||
|
||
void iovec_array_free(struct iovec *iov, size_t n) { | ||
if (!iov) | ||
return; | ||
|
||
for (size_t i = 0; i < n; i++) | ||
free(iov[i].iov_base); | ||
|
||
free(iov); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* SPDX-License-Identifier: LGPL-2.1-or-later */ | ||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <sys/types.h> | ||
#include <sys/uio.h> | ||
|
||
#include "macro.h" | ||
|
||
static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) { | ||
size_t r = 0; | ||
|
||
for (size_t j = 0; j < n; j++) | ||
r += i[j].iov_len; | ||
|
||
return r; | ||
} | ||
|
||
static inline bool IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) { | ||
/* Returns true if there is nothing else to send (bytes written cover all of the iovec), | ||
* false if there's still work to do. */ | ||
|
||
for (size_t j = 0; j < n; j++) { | ||
size_t sub; | ||
|
||
if (i[j].iov_len == 0) | ||
continue; | ||
if (k == 0) | ||
return false; | ||
|
||
sub = MIN(i[j].iov_len, k); | ||
i[j].iov_len -= sub; | ||
i[j].iov_base = (uint8_t*) i[j].iov_base + sub; | ||
k -= sub; | ||
} | ||
|
||
assert(k == 0); /* Anything else would mean that we wrote more bytes than available, | ||
* or the kernel reported writing more bytes than sent. */ | ||
return true; | ||
} | ||
|
||
#define IOVEC_NULL (struct iovec) {} | ||
#define IOVEC_MAKE(base, len) (struct iovec) { .iov_base = (base), .iov_len = (len) } | ||
#define IOVEC_MAKE_STRING(string) \ | ||
({ \ | ||
char *_s = (char*) (string); \ | ||
IOVEC_MAKE(_s, strlen(_s)); \ | ||
}) | ||
|
||
char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value); | ||
char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value); | ||
|
||
void iovec_array_free(struct iovec *iov, size_t n); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.