summaryrefslogtreecommitdiffstats
path: root/common/state/backend_bucket_cached.c
blob: 781ac2debd2a968a70a152f191e149852c532f57 (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
/*
 * Copyright (C) 2016 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <common.h>
#include "state.h"

struct state_backend_storage_bucket_cache {
	struct state_backend_storage_bucket bucket;

	struct state_backend_storage_bucket *raw;

	u8 *data;
	ssize_t data_len;
	bool force_write;

	/* For outputs */
	struct device_d *dev;
};

static inline struct state_backend_storage_bucket_cache
    *get_bucket_cache(struct state_backend_storage_bucket *bucket)
{
	return container_of(bucket,
			    struct state_backend_storage_bucket_cache,
			    bucket);
}

static inline void state_backend_bucket_cache_drop(
		struct state_backend_storage_bucket_cache *cache)
{
	if (cache->data) {
		free(cache->data);
		cache->data = NULL;
		cache->data_len = 0;
	}
}

static int state_backend_bucket_cache_fill(
		struct state_backend_storage_bucket_cache *cache)
{
	int ret;

	ret = cache->raw->read(cache->raw, &cache->data, &cache->data_len);
	if (ret == -EUCLEAN)
		cache->force_write = true;
	else if (ret)
		return ret;

	return 0;
}

static int state_backend_bucket_cache_read(struct state_backend_storage_bucket *bucket,
					   uint8_t ** buf_out,
					   ssize_t * len_hint)
{
	struct state_backend_storage_bucket_cache *cache =
			get_bucket_cache(bucket);
	int ret;

	if (!cache->data) {
		ret = state_backend_bucket_cache_fill(cache);
		if (ret)
			return ret;
	}

	if (cache->data) {
		*buf_out = xmemdup(cache->data, cache->data_len);
		if (!*buf_out)
			return -ENOMEM;
		*len_hint = cache->data_len;
	}

	return 0;
}

static int state_backend_bucket_cache_write(struct state_backend_storage_bucket *bucket,
					    const uint8_t * buf, ssize_t len)
{
	struct state_backend_storage_bucket_cache *cache =
			get_bucket_cache(bucket);
	int ret;

	if (!cache->force_write) {
		if (!cache->data)
			ret = state_backend_bucket_cache_fill(cache);

		if (cache->data_len == len && !memcmp(cache->data, buf, len))
			return 0;
	}

	state_backend_bucket_cache_drop(cache);

	ret = cache->raw->write(cache->raw, buf, len);
	if (ret)
		return ret;

	cache->data = xmemdup(buf, len);
	cache->data_len = len;
	return 0;
}

static int state_backend_bucket_cache_init(
		struct state_backend_storage_bucket *bucket)
{
	struct state_backend_storage_bucket_cache *cache =
			get_bucket_cache(bucket);

	if (cache->raw->init) {
		return cache->raw->init(cache->raw);
	}

	return 0;
}

static void state_backend_bucket_cache_free(
		struct state_backend_storage_bucket *bucket)
{
	struct state_backend_storage_bucket_cache *cache =
			get_bucket_cache(bucket);

	state_backend_bucket_cache_drop(cache);
	cache->raw->free(cache->raw);
	free(cache);
}

int state_backend_bucket_cached_create(struct device_d *dev,
				       struct state_backend_storage_bucket *raw,
				       struct state_backend_storage_bucket **out)
{
	struct state_backend_storage_bucket_cache *cache;

	cache = xzalloc(sizeof(*cache));
	cache->raw = raw;
	cache->dev = dev;

	cache->bucket.free = state_backend_bucket_cache_free;
	cache->bucket.read = state_backend_bucket_cache_read;
	cache->bucket.write = state_backend_bucket_cache_write;
	cache->bucket.init = state_backend_bucket_cache_init;

	*out = &cache->bucket;

	return 0;
}