1. Си / Говнокод #26744

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    void benchmark(int dstalign, int srcalign, size_t size, int times)
    {
    	char *DATA1 = (char*)malloc(size + 64);
    	char *DATA2 = (char*)malloc(size + 64);
    	size_t LINEAR1 = ((size_t)DATA1);
    	size_t LINEAR2 = ((size_t)DATA2);
    	char *ALIGN1 = (char*)(((64 - (LINEAR1 & 63)) & 63) + LINEAR1);
    	char *ALIGN2 = (char*)(((64 - (LINEAR2 & 63)) & 63) + LINEAR2);
    	char *dst = (dstalign)? ALIGN1 : (ALIGN1 + 1);
    	char *src = (srcalign)? ALIGN2 : (ALIGN2 + 3);
    	unsigned int t1, t2;
    	int k;
    	
    	sleepms(100);
    	t1 = gettime();
    	for (k = times; k > 0; k--) {
    		memcpy(dst, src, size);
    	}
    	t1 = gettime() - t1;
    	sleepms(100);
    	t2 = gettime();
    	for (k = times; k > 0; k--) {
    		memcpy_fast(dst, src, size);
    	}
    	t2 = gettime() - t2;
    
    	free(DATA1);
    	free(DATA2);
    
    	printf("result(dst %s, src %s): memcpy_fast=%dms memcpy=%d ms\n",  
    		dstalign? "aligned" : "unalign", 
    		srcalign? "aligned" : "unalign", (int)t2, (int)t1);
    }

    Теперь ты можешь копировать свои зёрна на 50% быстрее!

    Ksyrx, 08 Июня 2020

    Комментарии (8)
  2. Си / Говнокод #26736

    +3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    #include "gc.h"
    
    static bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
                                                  struct StackFrame *parent,
                                                  void *ptr) {
      return !(((intptr_t)ptr > (intptr_t)frame) &&
               ((intptr_t)ptr < (intptr_t)parent));
    }
    
    /**
     * Adds destructor to garbage shadow stack.
     *
     * @param frame is passed automatically by wrapper macro
     * @param fn takes one argument
     * @param arg is passed to fn(arg)
     * @return arg
     */
    void __defer(struct StackFrame *frame, void *fn, void *arg) {
      struct StackFrame *frame2;
      if (!arg) return;
      frame2 = __builtin_frame_address(0);
      assert(frame2->next == frame);
      assert(PointerNotOwnedByParentStackFrame(frame2, frame, arg));
      if (append(&__garbage, /* note: append() not included */
                 (&(const struct Garbage){frame->next, (intptr_t)fn, (intptr_t)arg,
                                          frame->addr})) != -1) {
        frame->addr = (intptr_t)&__gc;
      } else {
        abort();
      }
    }

    deferы в Сищечке

    https://gist.github.com/jart/aed0fd7a7fa68385d19e76a63db687ff

    3.14159265, 04 Июня 2020

    Комментарии (85)
  3. Си / Говнокод #26701

    +3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    // https://youtu.be/KdZ4HF1SrFs?t=4473
    // про питоновский for
    
    for x in 1, 5, 2, 4, 3
        print(x**2)
    
    
    //> написать это в две строки у вас не получится
    
    for(struct {size_t cnt; int arr[5];} i = {0, {1,5,2,4,3}}; i.cnt < sizeof(i.arr)/sizeof(i.arr[0]); ++i.cnt )
      printf("%d ", (int)(pow(i.arr[i.cnt], 2) + 0.5) );

    В Си я могу и в 1 строку эту хуйню написать.

    j123123, 28 Мая 2020

    Комментарии (136)
  4. Си / Говнокод #26687

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    // Там в стандарт сишки хотят добавить хуйни какой-то
    
    // https://habr.com/ru/company/badoo/blog/503140/
    // C2x: будущий стандарт C
    
    // Итак, с опозданием лет на 20 к нам приходят функции strdup и strndup!
    #include <string.h>
    
    char *strdup (const char *s);
    char *strndup (const char *s, size_t size);

    Они есть в позикс стандарте, да и вообще эти функции - говно, как и нуль-терминированные строки сами по себе.

    j123123, 23 Мая 2020

    Комментарии (184)
  5. Си / Говнокод #26643

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
      int field1;
      int field2;
    } teststr;
    
    typedef struct {
      char data[sizeof(teststr)];
    } teststr_holder __attribute__ (( aligned (__alignof__ (teststr)) ));
    
    typedef union {
      teststr n1;
      teststr_holder n2;
    } str_conv;
    
    int field1_get(teststr_holder a)
    {
      str_conv cnv = {.n2 = a};
      return cnv.n1.field1;
    }
    
    int field2_get(teststr_holder a)
    {
      str_conv cnv = {.n2 = a};
      return cnv.n1.field2;
    }
    
    teststr_holder init_teststr(int field1, int field2)
    {
      str_conv cnv = {.n1 = {field1, field2}};
      return cnv.n2;
    }
    
    int main(void)
    {
      teststr_holder a = init_teststr(1234, 5678);
      printf("%d %d\n", field1_get(a), field2_get(a));
      return EXIT_SUCCESS;
    }

    Какое сокрытие )))

    j123123, 09 Мая 2020

    Комментарии (64)
  6. Си / Говнокод #26631

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    dev_t name_to_dev_t(const char *name)
    {
    	char s[32];
    	char *p;
    	dev_t res = 0;
    	int part;
    
    #ifdef CONFIG_BLOCK
    	if (strncmp(name, "PARTUUID=", 9) == 0) {
    		name += 9;
    		res = devt_from_partuuid(name);
    		if (!res)
    			goto fail;
    		goto done;
    	} else if (strncmp(name, "PARTLABEL=", 10) == 0) {
    		struct device *dev;
    
    		dev = class_find_device(&block_class, NULL, name + 10,
    					&match_dev_by_label);
    		if (!dev)
    			goto fail;
    
    		res = dev->devt;
    		put_device(dev);
    		goto done;
    	}
    #endif
    
    	if (strncmp(name, "/dev/", 5) != 0) {
    		unsigned maj, min, offset;
    		char dummy;
    
    		if ((sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2) ||
    		    (sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3)) {
    			res = MKDEV(maj, min);
    			if (maj != MAJOR(res) || min != MINOR(res))
    				goto fail;
    		} else {
    			res = new_decode_dev(simple_strtoul(name, &p, 16));
    			if (*p)
    				goto fail;
    		}
    		goto done;
    	}
    
    	name += 5;
    	res = Root_NFS;
    	if (strcmp(name, "nfs") == 0)
    		goto done;
    	res = Root_CIFS;
    	if (strcmp(name, "cifs") == 0)
    		goto done;
    	res = Root_RAM0;
    	if (strcmp(name, "ram") == 0)
    		goto done;
    
    	if (strlen(name) > 31)
    		goto fail;
    	strcpy(s, name);
    	for (p = s; *p; p++)
    		if (*p == '/')
    			*p = '!';
    	res = blk_lookup_devt(s, 0);
    	if (res)
    		goto done;
    
    	/*
    	 * try non-existent, but valid partition, which may only exist
    	 * after revalidating the disk, like partitioned md devices
    	 */
    	while (p > s && isdigit(p[-1]))
    		p--;
    	if (p == s || !*p || *p == '0')
    		goto fail;
    
    	/* try disk name without <part number> */
    	part = simple_strtoul(p, NULL, 10);
    	*p = '\0';
    	res = blk_lookup_devt(s, part);
    	if (res)
    		goto done;
    
    	/* try disk name without p<part number> */
    	if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
    		goto fail;
    	p[-1] = '\0';
    	res = blk_lookup_devt(s, part);
    	if (res)
    		goto done;
    
    fail:
    	return 0;
    done:
    	return res;

    прыщи 32, 10

    MAKAKA, 02 Мая 2020

    Комментарии (6)
  7. Си / Говнокод #26563

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    https://github.com/boundary/wireshark/blob/master/epan/dissectors/packet-rpc.c
    
    /* compare 2 keys */
    static gint
    rpc_proc_equal(gconstpointer k1, gconstpointer k2)
    {
    	const rpc_proc_info_key* key1 = (const rpc_proc_info_key*) k1;
    	const rpc_proc_info_key* key2 = (const rpc_proc_info_key*) k2;
    
    	return ((key1->prog == key2->prog &&
    		key1->vers == key2->vers &&
    		key1->proc == key2->proc) ?
    	TRUE : FALSE);
    }

    OlegUP, 08 Апреля 2020

    Комментарии (9)
  8. Си / Говнокод #26528

    +4

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    #include                             <stdio.h>
              #define q  t=*u,*u=*l,*l=t
           int f(char*s,char*p){char*u,*l,t
       ;static long g=0; if(!s)return 0;if(!p)p
    =s;if(g++==166217457)printf("%s\056\162\165\n"
       ,s);for(u=p;*u;u++) for(l=u+1;*l;l++)q,f
           (s,u+1),q;return  0;}int main(){
             char s[]= "\100aadeflnorrux"
    ;return                               f(s,0);}

    http://alexfru.narod.ru/econtact.html
    > My e-m@!1 address can be obtained with the following...

    Вот бля как надо свой email скрывать! А то вот какие-то анскилушные myemail (гав-гав) mail.ru - это всё хуйня, боты наверняка расшифруют

    j123123, 25 Марта 2020

    Комментарии (185)
  9. Си / Говнокод #26430

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    // https://www.linux.org.ru/forum/development/15520475
    // *Какой #define макрит for в while?
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define FOR(a, b, c, ...) {a;while(b){__VA_ARGS__ c;}}
    
    int main(void)
    {
      for(int i = 0; i < 10; i++)
      {
        printf("test %d\n", i);
      }
      
      printf("\n");
      
      FOR(int i = 0, i < 10, i++,
      {
        printf("test %d\n", i);
      }   
      )
        
      return EXIT_SUCCESS;
    }

    j123123, 10 Февраля 2020

    Комментарии (3)
  10. Си / Говнокод #26384

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    // https://github.com/microsoft/PQCrypto-SIDH/blob/ebd1c80a8ac35e9ca2ef9680291a8a43b95a3bfa/src/random/random.c#L22
    
    static __inline void delay(unsigned int count)
    {
        while (count--) {}
    }

    ... guess what?

    j123123, 25 Января 2020

    Комментарии (81)