~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_iocache.c

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
  write buffer to the read buffer before we start to reuse it.
48
48
*/
49
49
 
50
 
#define MAP_TO_USE_RAID
51
50
#include "mysys_priv.h"
52
 
#include <m_string.h>
 
51
#include <mystrings/m_string.h>
53
52
#ifdef HAVE_AIOWAIT
54
53
#include "mysys_err.h"
 
54
#include <mysys/aio_result.h>
55
55
static void my_aiowait(my_aio_result *result);
56
56
#endif
 
57
#include <mysys/iocache.h>
57
58
#include <errno.h>
58
 
 
 
59
#include <drizzled/util/test.h>
59
60
#define lock_append_buffer(info) \
60
61
 pthread_mutex_lock(&(info)->append_buffer_lock)
61
62
#define unlock_append_buffer(info) \
152
153
  size_t min_cache;
153
154
  my_off_t pos;
154
155
  my_off_t end_of_file= ~(my_off_t) 0;
155
 
  DBUG_ENTER("init_io_cache");
156
 
  DBUG_PRINT("enter",("cache: 0x%lx  type: %d  pos: %ld",
157
 
                      (ulong) info, (int) type, (ulong) seek_offset));
158
156
 
159
157
  info->file= file;
160
158
  info->type= TYPE_NOT_SET;         /* Don't set it until mutex are created */
180
178
        the beginning of whatever this file is, then somebody made a bad
181
179
        assumption.
182
180
      */
183
 
      DBUG_ASSERT(seek_offset == 0);
 
181
      assert(seek_offset == 0);
184
182
    }
185
183
    else
186
184
      info->seek_not_done= test(seek_offset != pos);
190
188
  info->share=0;
191
189
 
192
190
  if (!cachesize && !(cachesize= my_default_record_cache_size))
193
 
    DBUG_RETURN(1);                             /* No cache requested */
 
191
    return(1);                          /* No cache requested */
194
192
  min_cache=use_async_io ? IO_SIZE*4 : IO_SIZE*2;
195
193
  if (type == READ_CACHE || type == SEQ_READ_APPEND)
196
194
  {                                             /* Assume file isn't growing */
224
222
      if (type == SEQ_READ_APPEND)
225
223
        buffer_block *= 2;
226
224
      if ((info->buffer=
227
 
           (uchar*) my_malloc(buffer_block,
 
225
           (unsigned char*) my_malloc(buffer_block,
228
226
                             MYF((cache_myflags & ~ MY_WME) |
229
227
                                 (cachesize == min_cache ? MY_WME : 0)))) != 0)
230
228
      {
235
233
        break;                                  /* Enough memory found */
236
234
      }
237
235
      if (cachesize == min_cache)
238
 
        DBUG_RETURN(2);                         /* Can't alloc cache */
 
236
        return(2);                              /* Can't alloc cache */
239
237
      /* Try with less memory */
240
238
      cachesize= (cachesize*3/4 & ~(min_cache-1));
241
239
    }
242
240
  }
243
241
 
244
 
  DBUG_PRINT("info",("init_io_cache: cachesize = %lu", (ulong) cachesize));
245
242
  info->read_length=info->buffer_length=cachesize;
246
243
  info->myflags=cache_myflags & ~(MY_NABP | MY_FNABP);
247
244
  info->request_pos= info->read_pos= info->write_pos = info->buffer;
255
252
  else
256
253
  {
257
254
    /* Clear mutex so that safe_mutex will notice that it's not initialized */
258
 
    bzero((char*) &info->append_buffer_lock, sizeof(info));
 
255
    memset(&info->append_buffer_lock, 0, sizeof(info));
259
256
  }
260
257
#endif
261
258
 
273
270
#ifdef HAVE_AIOWAIT
274
271
  if (use_async_io && ! my_disable_async_io)
275
272
  {
276
 
    DBUG_PRINT("info",("Using async io"));
277
273
    info->read_length/=2;
278
274
    info->read_function=_my_b_async_read;
279
275
  }
280
276
  info->inited=info->aio_result.pending=0;
281
277
#endif
282
 
  DBUG_RETURN(0);
 
278
  return(0);
283
279
}                                               /* init_io_cache */
284
280
 
285
281
        /* Wait until current request is ready */
296
292
      {
297
293
        if (errno == EINTR)
298
294
          continue;
299
 
        DBUG_PRINT("error",("No aio request, error: %d",errno));
300
295
        result->pending=0;                      /* Assume everythings is ok */
301
296
        break;
302
297
      }
322
317
                        bool use_async_io __attribute__((unused)),
323
318
                        bool clear_cache)
324
319
{
325
 
  DBUG_ENTER("reinit_io_cache");
326
 
  DBUG_PRINT("enter",("cache: 0x%lx type: %d  seek_offset: %lu  clear_cache: %d",
327
 
                      (ulong) info, type, (ulong) seek_offset,
328
 
                      (int) clear_cache));
329
 
 
330
320
  /* One can't do reinit with the following types */
331
 
  DBUG_ASSERT(type != READ_NET && info->type != READ_NET &&
 
321
  assert(type != READ_NET && info->type != READ_NET &&
332
322
              type != WRITE_NET && info->type != WRITE_NET &&
333
323
              type != SEQ_READ_APPEND && info->type != SEQ_READ_APPEND);
334
324
 
338
328
      seek_offset <= my_b_tell(info))
339
329
  {
340
330
    /* Reuse current buffer without flushing it to disk */
341
 
    uchar *pos;
 
331
    unsigned char *pos;
342
332
    if (info->type == WRITE_CACHE && type == READ_CACHE)
343
333
    {
344
334
      info->read_end=info->write_pos;
377
367
      info->end_of_file=my_b_tell(info);
378
368
    /* flush cache if we want to reuse it */
379
369
    if (!clear_cache && my_b_flush_io_cache(info,1))
380
 
      DBUG_RETURN(1);
 
370
      return(1);
381
371
    info->pos_in_file=seek_offset;
382
372
    /* Better to do always do a seek */
383
373
    info->seek_not_done=1;
399
389
 
400
390
#ifdef HAVE_AIOWAIT
401
391
  if (use_async_io && ! my_disable_async_io &&
402
 
      ((ulong) info->buffer_length <
403
 
       (ulong) (info->end_of_file - seek_offset)))
 
392
      ((uint32_t) info->buffer_length <
 
393
       (uint32_t) (info->end_of_file - seek_offset)))
404
394
  {
405
395
    info->read_length=info->buffer_length/2;
406
396
    info->read_function=_my_b_async_read;
407
397
  }
408
398
  info->inited=0;
409
399
#endif
410
 
  DBUG_RETURN(0);
 
400
  return(0);
411
401
} /* reinit_io_cache */
412
402
 
413
403
 
440
430
    1      Error: can't read requested characters
441
431
*/
442
432
 
443
 
int _my_b_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
 
433
int _my_b_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
444
434
{
445
435
  size_t length,diff_length,left_length, max_length;
446
436
  my_off_t pos_in_file;
447
 
  DBUG_ENTER("_my_b_read");
448
437
 
449
438
  if ((left_length= (size_t) (info->read_end-info->read_pos)))
450
439
  {
451
 
    DBUG_ASSERT(Count >= left_length);  /* User is not using my_b_read() */
 
440
    assert(Count >= left_length);       /* User is not using my_b_read() */
452
441
    memcpy(Buffer,info->read_pos, left_length);
453
442
    Buffer+=left_length;
454
443
    Count-=left_length;
478
467
        info->file is a pipe or socket or FIFO.  We never should have tried
479
468
        to seek on that.  See Bugs#25807 and #22828 for more info.
480
469
      */
481
 
      DBUG_ASSERT(my_errno != ESPIPE);
 
470
      assert(my_errno != ESPIPE);
482
471
      info->error= -1;
483
 
      DBUG_RETURN(1);
 
472
      return(1);
484
473
    }
485
474
  }
486
475
 
491
480
    if (info->end_of_file <= pos_in_file)
492
481
    {                                   /* End of file */
493
482
      info->error= (int) left_length;
494
 
      DBUG_RETURN(1);
 
483
      return(1);
495
484
    }
496
485
    length=(Count & (size_t) ~(IO_SIZE-1))-diff_length;
497
486
    if ((read_length= my_read(info->file,Buffer, length, info->myflags))
499
488
    {
500
489
      info->error= (read_length == (size_t) -1 ? -1 :
501
490
                    (int) (read_length+left_length));
502
 
      DBUG_RETURN(1);
 
491
      return(1);
503
492
    }
504
493
    Count-=length;
505
494
    Buffer+=length;
517
506
    if (Count)
518
507
    {
519
508
      info->error= left_length;         /* We only got this many char */
520
 
      DBUG_RETURN(1);
 
509
      return(1);
521
510
    }
522
511
    length=0;                           /* Didn't read any chars */
523
512
  }
530
519
    info->pos_in_file= pos_in_file;
531
520
    info->error= length == (size_t) -1 ? -1 : (int) (length+left_length);
532
521
    info->read_pos=info->read_end=info->buffer;
533
 
    DBUG_RETURN(1);
 
522
    return(1);
534
523
  }
535
524
  info->read_pos=info->buffer+Count;
536
525
  info->read_end=info->buffer+length;
537
526
  info->pos_in_file=pos_in_file;
538
527
  memcpy(Buffer, info->buffer, Count);
539
 
  DBUG_RETURN(0);
 
528
  return(0);
540
529
}
541
530
 
542
531
 
609
598
*/
610
599
 
611
600
void init_io_cache_share(IO_CACHE *read_cache, IO_CACHE_SHARE *cshare,
612
 
                         IO_CACHE *write_cache, uint num_threads)
 
601
                         IO_CACHE *write_cache, uint32_t num_threads)
613
602
{
614
 
  DBUG_ENTER("init_io_cache_share");
615
 
  DBUG_PRINT("io_cache_share", ("read_cache: 0x%lx  share: 0x%lx  "
616
 
                                "write_cache: 0x%lx  threads: %u",
617
 
                                (long) read_cache, (long) cshare,
618
 
                                (long) write_cache, num_threads));
619
 
 
620
 
  DBUG_ASSERT(num_threads > 1);
621
 
  DBUG_ASSERT(read_cache->type == READ_CACHE);
622
 
  DBUG_ASSERT(!write_cache || (write_cache->type == WRITE_CACHE));
 
603
  assert(num_threads > 1);
 
604
  assert(read_cache->type == READ_CACHE);
 
605
  assert(!write_cache || (write_cache->type == WRITE_CACHE));
623
606
 
624
607
  pthread_mutex_init(&cshare->mutex, MY_MUTEX_INIT_FAST);
625
608
  pthread_cond_init(&cshare->cond, 0);
641
624
  if (write_cache)
642
625
    write_cache->share= cshare;
643
626
 
644
 
  DBUG_VOID_RETURN;
 
627
  return;
645
628
}
646
629
 
647
630
 
667
650
void remove_io_thread(IO_CACHE *cache)
668
651
{
669
652
  IO_CACHE_SHARE *cshare= cache->share;
670
 
  uint total;
671
 
  DBUG_ENTER("remove_io_thread");
 
653
  uint32_t total;
672
654
 
673
655
  /* If the writer goes, it needs to flush the write cache. */
674
656
  if (cache == cshare->source_cache)
675
657
    flush_io_cache(cache);
676
658
 
677
659
  pthread_mutex_lock(&cshare->mutex);
678
 
  DBUG_PRINT("io_cache_share", ("%s: 0x%lx",
679
 
                                (cache == cshare->source_cache) ?
680
 
                                "writer" : "reader", (long) cache));
681
660
 
682
661
  /* Remove from share. */
683
662
  total= --cshare->total_threads;
684
 
  DBUG_PRINT("io_cache_share", ("remaining threads: %u", total));
685
663
 
686
664
  /* Detach from share. */
687
665
  cache->share= NULL;
689
667
  /* If the writer goes, let the readers know. */
690
668
  if (cache == cshare->source_cache)
691
669
  {
692
 
    DBUG_PRINT("io_cache_share", ("writer leaves"));
693
670
    cshare->source_cache= NULL;
694
671
  }
695
672
 
696
673
  /* If all threads are waiting for me to join the lock, wake them. */
697
674
  if (!--cshare->running_threads)
698
675
  {
699
 
    DBUG_PRINT("io_cache_share", ("the last running thread leaves, wake all"));
700
676
    pthread_cond_signal(&cshare->cond_writer);
701
677
    pthread_cond_broadcast(&cshare->cond);
702
678
  }
705
681
 
706
682
  if (!total)
707
683
  {
708
 
    DBUG_PRINT("io_cache_share", ("last thread removed, destroy share"));
709
684
    pthread_cond_destroy (&cshare->cond_writer);
710
685
    pthread_cond_destroy (&cshare->cond);
711
686
    pthread_mutex_destroy(&cshare->mutex);
712
687
  }
713
688
 
714
 
  DBUG_VOID_RETURN;
 
689
  return;
715
690
}
716
691
 
717
692
 
746
721
static int lock_io_cache(IO_CACHE *cache, my_off_t pos)
747
722
{
748
723
  IO_CACHE_SHARE *cshare= cache->share;
749
 
  DBUG_ENTER("lock_io_cache");
750
724
 
751
725
  /* Enter the lock. */
752
726
  pthread_mutex_lock(&cshare->mutex);
753
727
  cshare->running_threads--;
754
 
  DBUG_PRINT("io_cache_share", ("%s: 0x%lx  pos: %lu  running: %u",
755
 
                                (cache == cshare->source_cache) ?
756
 
                                "writer" : "reader", (long) cache, (ulong) pos,
757
 
                                cshare->running_threads));
758
728
 
759
729
  if (cshare->source_cache)
760
730
  {
765
735
      /* The writer waits until all readers are here. */
766
736
      while (cshare->running_threads)
767
737
      {
768
 
        DBUG_PRINT("io_cache_share", ("writer waits in lock"));
769
738
        pthread_cond_wait(&cshare->cond_writer, &cshare->mutex);
770
739
      }
771
 
      DBUG_PRINT("io_cache_share", ("writer awoke, going to copy"));
772
 
 
773
740
      /* Stay locked. Leave the lock later by unlock_io_cache(). */
774
 
      DBUG_RETURN(1);
 
741
      return(1);
775
742
    }
776
743
 
777
744
    /* The last thread wakes the writer. */
778
745
    if (!cshare->running_threads)
779
746
    {
780
 
      DBUG_PRINT("io_cache_share", ("waking writer"));
781
747
      pthread_cond_signal(&cshare->cond_writer);
782
748
    }
783
749
 
789
755
    while ((!cshare->read_end || (cshare->pos_in_file < pos)) &&
790
756
           cshare->source_cache)
791
757
    {
792
 
      DBUG_PRINT("io_cache_share", ("reader waits in lock"));
793
758
      pthread_cond_wait(&cshare->cond, &cshare->mutex);
794
759
    }
795
760
 
803
768
    */
804
769
    if (!cshare->read_end || (cshare->pos_in_file < pos))
805
770
    {
806
 
      DBUG_PRINT("io_cache_share", ("reader found writer removed. EOF"));
807
771
      cshare->read_end= cshare->buffer; /* Empty buffer. */
808
772
      cshare->error= 0; /* EOF is not an error. */
809
773
    }
816
780
    */
817
781
    if (!cshare->running_threads)
818
782
    {
819
 
      DBUG_PRINT("io_cache_share", ("last thread joined, going to read"));
820
783
      /* Stay locked. Leave the lock later by unlock_io_cache(). */
821
 
      DBUG_RETURN(1);
 
784
      return(1);
822
785
    }
823
786
 
824
787
    /*
831
794
    while ((!cshare->read_end || (cshare->pos_in_file < pos)) &&
832
795
           cshare->running_threads)
833
796
    {
834
 
      DBUG_PRINT("io_cache_share", ("reader waits in lock"));
835
797
      pthread_cond_wait(&cshare->cond, &cshare->mutex);
836
798
    }
837
799
 
838
800
    /* If the block is not yet read, continue with a locked cache and read. */
839
801
    if (!cshare->read_end || (cshare->pos_in_file < pos))
840
802
    {
841
 
      DBUG_PRINT("io_cache_share", ("reader awoke, going to read"));
842
803
      /* Stay locked. Leave the lock later by unlock_io_cache(). */
843
 
      DBUG_RETURN(1);
 
804
      return(1);
844
805
    }
845
806
 
846
807
    /* Another thread did read the block already. */
847
808
  }
848
 
  DBUG_PRINT("io_cache_share", ("reader awoke, going to process %u bytes",
849
 
                                (uint) (cshare->read_end ? (size_t)
850
 
                                        (cshare->read_end - cshare->buffer) :
851
 
                                        0)));
852
809
 
853
810
  /*
854
811
    Leave the lock. Do not call unlock_io_cache() later. The thread that
855
812
    filled the buffer did this and marked all threads as running.
856
813
  */
857
814
  pthread_mutex_unlock(&cshare->mutex);
858
 
  DBUG_RETURN(0);
 
815
  return(0);
859
816
}
860
817
 
861
818
 
889
846
static void unlock_io_cache(IO_CACHE *cache)
890
847
{
891
848
  IO_CACHE_SHARE *cshare= cache->share;
892
 
  DBUG_ENTER("unlock_io_cache");
893
 
  DBUG_PRINT("io_cache_share", ("%s: 0x%lx  pos: %lu  running: %u",
894
 
                                (cache == cshare->source_cache) ?
895
 
                                "writer" : "reader",
896
 
                                (long) cache, (ulong) cshare->pos_in_file,
897
 
                                cshare->total_threads));
898
849
 
899
850
  cshare->running_threads= cshare->total_threads;
900
851
  pthread_cond_broadcast(&cshare->cond);
901
852
  pthread_mutex_unlock(&cshare->mutex);
902
 
  DBUG_VOID_RETURN;
 
853
  return;
903
854
}
904
855
 
905
856
 
940
891
    1      Error: can't read requested characters
941
892
*/
942
893
 
943
 
int _my_b_read_r(register IO_CACHE *cache, uchar *Buffer, size_t Count)
 
894
int _my_b_read_r(register IO_CACHE *cache, unsigned char *Buffer, size_t Count)
944
895
{
945
896
  my_off_t pos_in_file;
946
897
  size_t length, diff_length, left_length;
947
898
  IO_CACHE_SHARE *cshare= cache->share;
948
 
  DBUG_ENTER("_my_b_read_r");
949
899
 
950
900
  if ((left_length= (size_t) (cache->read_end - cache->read_pos)))
951
901
  {
952
 
    DBUG_ASSERT(Count >= left_length);  /* User is not using my_b_read() */
 
902
    assert(Count >= left_length);       /* User is not using my_b_read() */
953
903
    memcpy(Buffer, cache->read_pos, left_length);
954
904
    Buffer+= left_length;
955
905
    Count-= left_length;
970
920
    if (length == 0)
971
921
    {
972
922
      cache->error= (int) left_length;
973
 
      DBUG_RETURN(1);
 
923
      return(1);
974
924
    }
975
925
    if (lock_io_cache(cache, pos_in_file))
976
926
    {
977
927
      /* With a synchronized write/read cache we won't come here... */
978
 
      DBUG_ASSERT(!cshare->source_cache);
 
928
      assert(!cshare->source_cache);
979
929
      /*
980
930
        ... unless the writer has gone before this thread entered the
981
931
        lock. Simulate EOF in this case. It can be distinguished by
998
948
          {
999
949
            cache->error= -1;
1000
950
            unlock_io_cache(cache);
1001
 
            DBUG_RETURN(1);
 
951
            return(1);
1002
952
          }
1003
953
        }
1004
954
        len= my_read(cache->file, cache->buffer, length, cache->myflags);
1005
955
      }
1006
 
      DBUG_PRINT("io_cache_share", ("read %lu bytes", (ulong) len));
1007
 
 
1008
956
      cache->read_end=    cache->buffer + (len == (size_t) -1 ? 0 : len);
1009
957
      cache->error=       (len == length ? 0 : (int) len);
1010
958
      cache->pos_in_file= pos_in_file;
1034
982
    cache->seek_not_done= 0;
1035
983
    if (len == 0 || len == (size_t) -1)
1036
984
    {
1037
 
      DBUG_PRINT("io_cache_share", ("reader error. len %lu  left %lu",
1038
 
                                    (ulong) len, (ulong) left_length));
1039
985
      cache->error= (int) left_length;
1040
 
      DBUG_RETURN(1);
 
986
      return(1);
1041
987
    }
1042
988
    cnt= (len > Count) ? Count : len;
1043
989
    memcpy(Buffer, cache->read_pos, cnt);
1046
992
    left_length+= cnt;
1047
993
    cache->read_pos+= cnt;
1048
994
  }
1049
 
  DBUG_RETURN(0);
 
995
  return(0);
1050
996
}
1051
997
 
1052
998
 
1068
1014
*/
1069
1015
 
1070
1016
static void copy_to_read_buffer(IO_CACHE *write_cache,
1071
 
                                const uchar *write_buffer, size_t write_length)
 
1017
                                const unsigned char *write_buffer, size_t write_length)
1072
1018
{
1073
1019
  IO_CACHE_SHARE *cshare= write_cache->share;
1074
1020
 
1075
 
  DBUG_ASSERT(cshare->source_cache == write_cache);
 
1021
  assert(cshare->source_cache == write_cache);
1076
1022
  /*
1077
1023
    write_length is usually less or equal to buffer_length.
1078
1024
    It can be bigger if _my_b_write() is called with a big length.
1079
1025
  */
1080
1026
  while (write_length)
1081
1027
  {
1082
 
    size_t copy_length= min(write_length, write_cache->buffer_length);
 
1028
    size_t copy_length= cmin(write_length, write_cache->buffer_length);
1083
1029
    int  __attribute__((unused)) rc;
1084
1030
 
1085
1031
    rc= lock_io_cache(write_cache, write_cache->pos_in_file);
1086
1032
    /* The writing thread does always have the lock when it awakes. */
1087
 
    DBUG_ASSERT(rc);
 
1033
    assert(rc);
1088
1034
 
1089
1035
    memcpy(cshare->buffer, write_buffer, copy_length);
1090
1036
 
1114
1060
    1  Failed to read
1115
1061
*/
1116
1062
 
1117
 
int _my_b_seq_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
 
1063
int _my_b_seq_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
1118
1064
{
1119
1065
  size_t length, diff_length, left_length, save_count, max_length;
1120
1066
  my_off_t pos_in_file;
1123
1069
  /* first, read the regular buffer */
1124
1070
  if ((left_length=(size_t) (info->read_end-info->read_pos)))
1125
1071
  {
1126
 
    DBUG_ASSERT(Count > left_length);   /* User is not using my_b_read() */
 
1072
    assert(Count > left_length);        /* User is not using my_b_read() */
1127
1073
    memcpy(Buffer,info->read_pos, left_length);
1128
1074
    Buffer+=left_length;
1129
1075
    Count-=left_length;
1205
1151
 
1206
1152
      /*
1207
1153
         added the line below to make
1208
 
         DBUG_ASSERT(pos_in_file==info->end_of_file) pass.
 
1154
         assert(pos_in_file==info->end_of_file) pass.
1209
1155
         otherwise this does not appear to be needed
1210
1156
      */
1211
1157
      pos_in_file += length;
1232
1178
    size_t copy_len;
1233
1179
    size_t transfer_len;
1234
1180
 
1235
 
    DBUG_ASSERT(info->append_read_pos <= info->write_pos);
 
1181
    assert(info->append_read_pos <= info->write_pos);
1236
1182
    /*
1237
1183
      TODO: figure out if the assert below is needed or correct.
1238
1184
    */
1239
 
    DBUG_ASSERT(pos_in_file == info->end_of_file);
1240
 
    copy_len=min(Count, len_in_buff);
 
1185
    assert(pos_in_file == info->end_of_file);
 
1186
    copy_len=cmin(Count, len_in_buff);
1241
1187
    memcpy(Buffer, info->append_read_pos, copy_len);
1242
1188
    info->append_read_pos += copy_len;
1243
1189
    Count -= copy_len;
1276
1222
     1          An error has occurred; IO_CACHE to error state.
1277
1223
*/
1278
1224
 
1279
 
int _my_b_async_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
 
1225
int _my_b_async_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
1280
1226
{
1281
1227
  size_t length,read_length,diff_length,left_length,use_length,org_Count;
1282
1228
  size_t max_length;
1283
1229
  my_off_t next_pos_in_file;
1284
 
  uchar *read_buffer;
 
1230
  unsigned char *read_buffer;
1285
1231
 
1286
1232
  memcpy(Buffer,info->read_pos,
1287
1233
         (left_length= (size_t) (info->read_end-info->read_pos)));
1338
1284
        read_length-=offset;                    /* Bytes left from read_pos */
1339
1285
      }
1340
1286
    }
1341
 
#ifndef DBUG_OFF
1342
 
    if (info->aio_read_pos > info->pos_in_file)
1343
 
    {
1344
 
      my_errno=EINVAL;
1345
 
      return(info->read_length= (size_t) -1);
1346
 
    }
1347
 
#endif
1348
1287
        /* Copy found bytes to buffer */
1349
 
    length=min(Count,read_length);
 
1288
    length=cmin(Count,read_length);
1350
1289
    memcpy(Buffer,info->read_pos,(size_t) length);
1351
1290
    Buffer+=length;
1352
1291
    Count-=length;
1380
1319
      if ((read_length=my_read(info->file,info->request_pos,
1381
1320
                               read_length, info->myflags)) == (size_t) -1)
1382
1321
        return info->error= -1;
1383
 
      use_length=min(Count,read_length);
 
1322
      use_length=cmin(Count,read_length);
1384
1323
      memcpy(Buffer,info->request_pos,(size_t) use_length);
1385
1324
      info->read_pos=info->request_pos+Count;
1386
1325
      info->read_end=info->request_pos+read_length;
1423
1362
  if (max_length)
1424
1363
  {
1425
1364
    info->aio_result.result.aio_errno=AIO_INPROGRESS;   /* Marker for test */
1426
 
    DBUG_PRINT("aioread",("filepos: %ld  length: %lu",
1427
 
                          (ulong) next_pos_in_file, (ulong) max_length));
1428
1365
    if (aioread(info->file,read_buffer, max_length,
1429
1366
                (my_off_t) next_pos_in_file,MY_SEEK_SET,
1430
1367
                &info->aio_result.result))
1431
1368
    {                                           /* Skip async io */
1432
1369
      my_errno=errno;
1433
 
      DBUG_PRINT("error",("got error: %d, aio_result: %d from aioread, async skipped",
1434
 
                          errno, info->aio_result.result.aio_errno));
1435
1370
      if (info->request_pos != info->buffer)
1436
1371
      {
1437
 
        bmove(info->buffer,info->request_pos,
1438
 
              (size_t) (info->read_end - info->read_pos));
 
1372
        memcpy(info->buffer, info->request_pos,
 
1373
               (size_t) (info->read_end - info->read_pos));
1439
1374
        info->request_pos=info->buffer;
1440
1375
        info->read_pos-=info->read_length;
1441
1376
        info->read_end-=info->read_length;
1455
1390
 
1456
1391
int _my_b_get(IO_CACHE *info)
1457
1392
{
1458
 
  uchar buff;
 
1393
  unsigned char buff;
1459
1394
  IO_CACHE_CALLBACK pre_read,post_read;
1460
1395
  if ((pre_read = info->pre_read))
1461
1396
    (*pre_read)(info);
1463
1398
    return my_b_EOF;
1464
1399
  if ((post_read = info->post_read))
1465
1400
    (*post_read)(info);
1466
 
  return (int) (uchar) buff;
 
1401
  return (int) (unsigned char) buff;
1467
1402
}
1468
1403
 
1469
1404
/* 
1476
1411
   -1 On error; my_errno contains error code.
1477
1412
*/
1478
1413
 
1479
 
int _my_b_write(register IO_CACHE *info, const uchar *Buffer, size_t Count)
 
1414
int _my_b_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count)
1480
1415
{
1481
1416
  size_t rest_length,length;
1482
1417
 
1545
1480
  the write buffer before we are ready with it.
1546
1481
*/
1547
1482
 
1548
 
int my_b_append(register IO_CACHE *info, const uchar *Buffer, size_t Count)
 
1483
int my_b_append(register IO_CACHE *info, const unsigned char *Buffer, size_t Count)
1549
1484
{
1550
1485
  size_t rest_length,length;
1551
1486
 
1553
1488
    Assert that we cannot come here with a shared cache. If we do one
1554
1489
    day, we might need to add a call to copy_to_read_buffer().
1555
1490
  */
1556
 
  DBUG_ASSERT(!info->share);
 
1491
  assert(!info->share);
1557
1492
 
1558
1493
  lock_append_buffer(info);
1559
1494
  rest_length= (size_t) (info->write_end - info->write_pos);
1589
1524
}
1590
1525
 
1591
1526
 
1592
 
int my_b_safe_write(IO_CACHE *info, const uchar *Buffer, size_t Count)
 
1527
int my_b_safe_write(IO_CACHE *info, const unsigned char *Buffer, size_t Count)
1593
1528
{
1594
1529
  /*
1595
1530
    Sasha: We are not writing this with the ? operator to avoid hitting
1609
1544
  we will never get a seek over the end of the buffer
1610
1545
*/
1611
1546
 
1612
 
int my_block_write(register IO_CACHE *info, const uchar *Buffer, size_t Count,
 
1547
int my_block_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count,
1613
1548
                   my_off_t pos)
1614
1549
{
1615
1550
  size_t length;
1619
1554
    Assert that we cannot come here with a shared cache. If we do one
1620
1555
    day, we might need to add a call to copy_to_read_buffer().
1621
1556
  */
1622
 
  DBUG_ASSERT(!info->share);
 
1557
  assert(!info->share);
1623
1558
 
1624
1559
  if (pos < info->pos_in_file)
1625
1560
  {
1674
1609
  size_t length;
1675
1610
  bool append_cache;
1676
1611
  my_off_t pos_in_file;
1677
 
  DBUG_ENTER("my_b_flush_io_cache");
1678
 
  DBUG_PRINT("enter", ("cache: 0x%lx", (long) info));
1679
1612
 
1680
1613
  if (!(append_cache = (info->type == SEQ_READ_APPEND)))
1681
1614
    need_append_buffer_lock=0;
1685
1618
    if (info->file == -1)
1686
1619
    {
1687
1620
      if (real_open_cached_file(info))
1688
 
        DBUG_RETURN((info->error= -1));
 
1621
        return((info->error= -1));
1689
1622
    }
1690
1623
    LOCK_APPEND_BUFFER;
1691
1624
 
1711
1644
            MY_FILEPOS_ERROR)
1712
1645
        {
1713
1646
          UNLOCK_APPEND_BUFFER;
1714
 
          DBUG_RETURN((info->error= -1));
 
1647
          return((info->error= -1));
1715
1648
        }
1716
1649
        if (!append_cache)
1717
1650
          info->seek_not_done=0;
1733
1666
      else
1734
1667
      {
1735
1668
        info->end_of_file+=(info->write_pos-info->append_read_pos);
1736
 
        DBUG_ASSERT(info->end_of_file == my_tell(info->file,MYF(0)));
 
1669
        assert(info->end_of_file == my_tell(info->file,MYF(0)));
1737
1670
      }
1738
1671
 
1739
1672
      info->append_read_pos=info->write_pos=info->write_buffer;
1740
1673
      ++info->disk_writes;
1741
1674
      UNLOCK_APPEND_BUFFER;
1742
 
      DBUG_RETURN(info->error);
 
1675
      return(info->error);
1743
1676
    }
1744
1677
  }
1745
1678
#ifdef HAVE_AIOWAIT
1750
1683
  }
1751
1684
#endif
1752
1685
  UNLOCK_APPEND_BUFFER;
1753
 
  DBUG_RETURN(0);
 
1686
  return(0);
1754
1687
}
1755
1688
 
1756
1689
/*
1774
1707
{
1775
1708
  int error=0;
1776
1709
  IO_CACHE_CALLBACK pre_close;
1777
 
  DBUG_ENTER("end_io_cache");
1778
 
  DBUG_PRINT("enter",("cache: 0x%lx", (ulong) info));
1779
1710
 
1780
1711
  /*
1781
1712
    Every thread must call remove_io_thread(). The last one destroys
1782
1713
    the share elements.
1783
1714
  */
1784
 
  DBUG_ASSERT(!info->share || !info->share->total_threads);
 
1715
  assert(!info->share || !info->share->total_threads);
1785
1716
 
1786
1717
  if ((pre_close=info->pre_close))
1787
1718
  {
1793
1724
    info->alloced_buffer=0;
1794
1725
    if (info->file != -1)                       /* File doesn't exist */
1795
1726
      error= my_b_flush_io_cache(info,1);
1796
 
    my_free((uchar*) info->buffer,MYF(MY_WME));
1797
 
    info->buffer=info->read_pos=(uchar*) 0;
 
1727
    free((unsigned char*) info->buffer);
 
1728
    info->buffer=info->read_pos=(unsigned char*) 0;
1798
1729
  }
1799
1730
  if (info->type == SEQ_READ_APPEND)
1800
1731
  {
1802
1733
    info->type= TYPE_NOT_SET;
1803
1734
    pthread_mutex_destroy(&info->append_buffer_lock);
1804
1735
  }
1805
 
  DBUG_RETURN(error);
 
1736
  return(error);
1806
1737
} /* end_io_cache */
1807
1738
 
1808
1739
 
1877
1808
    total_bytes += 4+block_size;
1878
1809
  }
1879
1810
  close_file(&sra_cache);
1880
 
  my_free(block,MYF(MY_WME));
 
1811
  free(block);
1881
1812
  if (!my_stat(fname,&status,MYF(MY_WME)))
1882
1813
    die("%s failed to stat, but I had just closed it,\
1883
1814
 wonder how that happened");