trunk/src/emu/softlist.c
| r243280 | r243281 | |
| 265 | 265 | |
| 266 | 266 | |
| 267 | 267 | //************************************************************************** |
| 268 | | // CONST STRING POOL |
| 269 | | //************************************************************************** |
| 270 | | |
| 271 | | //------------------------------------------------- |
| 272 | | // const_string_pool - constructor |
| 273 | | //------------------------------------------------- |
| 274 | | |
| 275 | | const_string_pool::const_string_pool() |
| 276 | | { |
| 277 | | } |
| 278 | | |
| 279 | | |
| 280 | | //------------------------------------------------- |
| 281 | | // add - add a string to the string pool |
| 282 | | //------------------------------------------------- |
| 283 | | |
| 284 | | const char *const_string_pool::add(const char *string) |
| 285 | | { |
| 286 | | // if NULL or a small number (for some hash strings), just return as-is |
| 287 | | if (FPTR(string) < 0x100) |
| 288 | | return string; |
| 289 | | |
| 290 | | // scan to find space |
| 291 | | for (pool_chunk *chunk = m_chunklist.first(); chunk != NULL; chunk = chunk->next()) |
| 292 | | { |
| 293 | | const char *result = chunk->add(string); |
| 294 | | if (result != NULL) |
| 295 | | return result; |
| 296 | | } |
| 297 | | |
| 298 | | // no space anywhere, create a new pool and prepend it (so it gets used first) |
| 299 | | const char *result = m_chunklist.prepend(*global_alloc(pool_chunk)).add(string); |
| 300 | | assert(result != NULL); |
| 301 | | return result; |
| 302 | | } |
| 303 | | |
| 304 | | |
| 305 | | //------------------------------------------------- |
| 306 | | // contains - determine if the given string |
| 307 | | // pointer lives in the pool |
| 308 | | //------------------------------------------------- |
| 309 | | |
| 310 | | bool const_string_pool::contains(const char *string) |
| 311 | | { |
| 312 | | // if NULL or a small number (for some hash strings), then yes, effectively |
| 313 | | if (FPTR(string) < 0x100) |
| 314 | | return true; |
| 315 | | |
| 316 | | // scan to find it |
| 317 | | for (pool_chunk *chunk = m_chunklist.first(); chunk != NULL; chunk = chunk->next()) |
| 318 | | if (chunk->contains(string)) |
| 319 | | return true; |
| 320 | | |
| 321 | | return false; |
| 322 | | } |
| 323 | | |
| 324 | | |
| 325 | | //------------------------------------------------- |
| 326 | | // pool_chunk - constructor |
| 327 | | //------------------------------------------------- |
| 328 | | |
| 329 | | const_string_pool::pool_chunk::pool_chunk() |
| 330 | | : m_next(NULL), |
| 331 | | m_used(0) |
| 332 | | { |
| 333 | | } |
| 334 | | |
| 335 | | |
| 336 | | //------------------------------------------------- |
| 337 | | // add - add a string to this pool |
| 338 | | //------------------------------------------------- |
| 339 | | |
| 340 | | const char *const_string_pool::pool_chunk::add(const char *string) |
| 341 | | { |
| 342 | | // get the length of the string (no string can be longer than a full pool) |
| 343 | | int bytes = strlen(string) + 1; |
| 344 | | assert(bytes < POOL_SIZE); |
| 345 | | |
| 346 | | // if too big, return NULL |
| 347 | | if (m_used + bytes > POOL_SIZE) |
| 348 | | return NULL; |
| 349 | | |
| 350 | | // allocate, copy, and return the memory |
| 351 | | char *dest = &m_buffer[m_used]; |
| 352 | | m_used += bytes; |
| 353 | | memcpy(dest, string, bytes); |
| 354 | | return dest; |
| 355 | | } |
| 356 | | |
| 357 | | |
| 358 | | |
| 359 | | //************************************************************************** |
| 360 | 268 | // SOFTWARE LIST DEVICE |
| 361 | 269 | //************************************************************************** |
| 362 | 270 | |