Guest User

Untitled

a guest
Apr 23rd, 2016
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5.  
  6. typedef struct {
  7.     int addr;
  8. } sl_server_t;
  9.  
  10. typedef struct {
  11.     sl_server_t *servers;
  12.     size_t count;
  13.     size_t allocated_len;
  14. } server_list_t;
  15.  
  16. server_list_t server_list;
  17.  
  18. void ServerList_Init(size_t count) {
  19.     if (count < 1) count = 1;
  20.     server_list.servers = malloc(count * sizeof(*server_list.servers));
  21.     if (!server_list.servers) {
  22.         perror("realloc");
  23.         exit(1);
  24.     }
  25.     server_list.allocated_len = count * sizeof(*server_list.servers);
  26.     server_list.count = 0;
  27. }
  28.  
  29. void ServerList_Add(sl_server_t server) {
  30.     for (register size_t i = 0; i < server_list.count; i++) {
  31.         if (server_list.servers[i].addr == server.addr)
  32.             return;
  33.     }
  34.  
  35.     if (server_list.count > server_list.allocated_len / sizeof(*server_list.servers)) {
  36.         server_list.servers = realloc(server_list.servers, server_list.count * sizeof(*server_list.servers) * 2);
  37.         if (!server_list.servers) {
  38.             perror("realloc");
  39.             exit(1);
  40.         }
  41.         server_list.allocated_len = server_list.count * sizeof(*server_list.servers) * 2;
  42.     }
  43.  
  44.     server_list.servers[server_list.count] = server;
  45.     server_list.count++;
  46. }
  47.  
  48. void ServerList_Remove(sl_server_t server) {
  49.     for (register size_t i = 0; i < server_list.count; i++) {
  50.         if (server_list.servers[i].addr == server.addr) {
  51.             server_list.servers[i] = server_list.servers[server_list.count - 1];
  52.             memset(&server_list.servers[server_list.count - 1], 0, sizeof(sl_server_t));
  53.             server_list.count--;
  54.         }
  55.     }
  56. }
  57.  
  58. sl_server_t* ServerList_GetListPtr() {
  59.     return server_list.servers;
  60. }
  61.  
  62. int main() {
  63.     ServerList_Init(2);
  64.  
  65.     sl_server_t srv;
  66.     for (size_t i = 1; i < 100000; i++) {
  67.         srv.addr = i;
  68.         ServerList_Add(srv);
  69.     }
  70.  
  71.     sl_server_t *servers = ServerList_GetListPtr();
  72.     size_t count = 0;
  73.     while (servers->addr) {
  74.         servers++;
  75.         count++;
  76.     }
  77.     printf("%d\n", count);
  78.  
  79.     for (size_t i = 1; i < 10000; i++) {
  80.         srv.addr = i;
  81.         ServerList_Remove(srv);
  82.     }
  83.  
  84.     for (size_t i = 10000; i < 100000; i++) {
  85.         srv.addr = i;
  86.         ServerList_Add(srv);
  87.     }
  88.  
  89.     for (size_t i = 10000; i < 100000; i++) {
  90.         srv.addr = i;
  91.         ServerList_Remove(srv);
  92.     }
  93.  
  94.     servers = ServerList_GetListPtr();
  95.     count = 0;
  96.     while (servers->addr) {
  97.         servers++;
  98.         count++;
  99.     }
  100.     printf("ok: %d servers", count);
  101. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×