| 165 | #include "swoc/Scalar.h" |
| 166 | |
| 167 | HostDBRecord * |
| 168 | HostDBRecord::alloc(swoc::TextView query_name, unsigned int rr_count, size_t srv_name_size) |
| 169 | { |
| 170 | const swoc::Scalar<8, ssize_t> qn_size = swoc::round_up(query_name.size() + 1); |
| 171 | const swoc::Scalar<8, ssize_t> r_size = |
| 172 | swoc::round_up(sizeof(self_type) + qn_size + rr_count * sizeof(HostDBInfo) + srv_name_size); |
| 173 | auto ptr = malloc(r_size); |
| 174 | memset(ptr, 0, r_size); |
| 175 | auto self = static_cast<self_type *>(ptr); |
| 176 | new (self) self_type(); |
| 177 | self->_iobuffer_index = 0; |
| 178 | self->_record_size = r_size; |
| 179 | |
| 180 | Dbg(dbg_ctl_hostdb, "allocating %ld bytes for %.*s with %d RR records at [%p]", r_size.value(), int(query_name.size()), |
| 181 | query_name.data(), rr_count, self); |
| 182 | |
| 183 | // where in our block of memory we are |
| 184 | int offset = sizeof(self_type); |
| 185 | memcpy(self->apply_offset<void>(offset), query_name); |
| 186 | offset += qn_size; |
| 187 | self->rr_offset = offset; |
| 188 | self->rr_count = rr_count; |
| 189 | // Construct the info instances to a valid state. |
| 190 | for (auto &info : self->rr_info()) { |
| 191 | new (&info) std::remove_reference_t<decltype(info)>; |
| 192 | } |
| 193 | |
| 194 | return self; |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | HostDBRecord::free() |